MCP is a Go implementation of the Model Context Protocol — a standardized way for applications to communicate with AI models. It allows developers to seamlessly bridge applications and AI models using a lightweight, JSON-RPC–based protocol.
This repository contains the shared protocol definitions and schemas for MCP. It is used by MCP, which provides the actual implementation of the MCP server and client framework.
Official Model Context Protocol Specification
MCP (Model Context Protocol) is designed to provide a standardized communication layer between applications and AI models. The protocol simplifies the integration of AI capabilities into applications by offering a consistent interface for resource access, prompt management, model interaction, and tool invocation.
Key features:
- JSON-RPC 2.0–based communication
- Support for multiple transport protocols (HTTP/SSE, stdio)
- Server-side features:
- Resource management
- Model prompting and completion
- Tool invocation
- Subscriptions for resource updates
- Logging
- Progress reporting
- Request cancellation
- Client-side features:
- Roots
- Sampling
For detailed guides on custom implementers and authentication, see docs/implementer.md and docs/authentication.md.
MCP is built around the following components:
- Server: Handles incoming requests and dispatches to implementer
- Client: Makes requests to MCP-compatible servers
- Implementer: Provides the actual functionality behind each protocol method
graph LR
Client[MCP Client] -->|JSON-RPC / HTTP/SSE| Server[MCP Server]
Server -->|Dispatches to| Implementer[MCP Implementer]
subgraph Auth[Authentication / Authorization]
OAuth2[OAuth2 / OIDC]
end
Client -.->|Bearer Token| OAuth2
Server -.->|Token Validation| OAuth2
go get github.com/viant/mcp
If you just need to connect **existing tools** to a remote MCP server you might prefer to use the standalone **Bridge** binary instead of embedding the Go package. See [Bridge Guide](docs/bridge.md) for details.
Default implementer can be used to quickly set up an MCP server. This implementer provides no-op stubs for all methods, allowing you to focus on implementing only the methods you need. Register handlers inline without writing a custom implementer type:
package main
import (
"context"
"fmt"
"github.com/viant/jsonrpc"
"github.com/viant/mcp-protocol/schema"
serverproto "github.com/viant/mcp-protocol/server"
"github.com/viant/mcp/server"
"log"
)
func main() {
type Addition struct {
A int `json:"a"`
B int `json:"b"`
}
newImplementer := serverproto.WithDefaultImplementer(context.Background(), func(implementer *serverproto.DefaultImplementer) error {
// Register a simple resource
implementer.RegisterResource(schema.Resource{Name: "hello", Uri: "/hello"},
func(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error) {
return &schema.ReadResourceResult{Contents: []schema.ReadResourceResultContentsElem{{Text: "Hello, world!"}}}, nil
})
// Register a simple calculator tool: adds two integers
if err := serverproto.RegisterTool[*Addition](implementer, "add", "Add two integers", func(ctx context.Context, input *Addition) (*schema.CallToolResult, *jsonrpc.Error) {
sum := input.A + input.B
return &schema.CallToolResult{Content: []schema.CallToolResultContentElem{{Text: fmt.Sprintf("%d", sum)}}}, nil
}); err != nil {
return err
}
return nil
})
srv, err := server.New(
server.WithNewImplementer(newImplementer),
server.WithImplementation(schema.Implementation{"default", "1.0"}),
)
if err != nil {
log.Fatalf("Failed to create server: %v", err)
}
log.Fatal(srv.HTTP(context.Background(), ":4981").ListenAndServe())
}
- Implementer Guide: docs/implementer.md
- Authentication Guide: docs/authentication.md
- Bridge (local proxy) Guide: docs/bridge.md
MCP supports transport-agnostic authentication and authorization (HTTP or HTTP-SSE) via OAuth2/OIDC in two modes:
-
Global Resource Protection (spec-based):
github.com/viant/mcp/server/auth.AuthServer
enforces a Bearer token across all endpoints, except those excluded viaExcludeURI
(e.g./sse
).- Configure by creating an
auth.Service
fromauthorization.Policy
and wiring it with:server.WithProtectedResourcesHandler(service.ProtectedResourcesHandler)
,server.WithAuthorizer(service.Middleware)
, andserver.WithJRPCAuthorizer(service.EnsureAuthorized)
. - Exposes
/.well-known/oauth-protected-resource
for metadata discovery (RFC 9728).
-
Fine-Grained Tool/Resource Control (experimental):
- Implements
auth.Authorizer
inAuthServer.EnsureAuthorized
, returning401 Unauthorized
per JSON-RPC request. - Configure per-tool/resource metadata via
Config.Tools
orConfig.Tenants
inauthorization.Policy
.
- Implements
-
Fallback Token Fetching:
github.com/viant/mcp/server/auth.FallbackAuth
wraps a strictAuthServer
.- On
401
challenge, fetches tokens viaProtectedResourceTokenSource
and optional ID tokens viaIdTokenSource
, then retries. - Create with
auth.NewFallbackAuth(strictAuthServer, tokenSource, idTokenSource)
.
-
Client-Side Support:
- Use
github.com/viant/mcp/client/auth/transport.New
with:WithStore(store.Store)
: handles client config, metadata & token caching.WithAuthFlow(flow.AuthFlow)
: selects interactive auth flow (e.g., browser PKCE).
- The RoundTripper automatically:
- Handles the initial
401
challenge (WWW-Authenticate header). - Discovers protected resource and auth server metadata.
- Acquires tokens and retries the original request with
Authorization: Bearer <token>
.
- Handles the initial
- Use
-
Example SSE integration:
rt, _ := transport.New( transport.WithStore(myStore), transport.WithAuthFlow(flow.NewBrowserFlow()), ) httpClient := &http.Client{Transport: rt} sseTransport, _ := sse.New(ctx, "https://myapp.example.com/sse", sse.WithClient(httpClient)) mcpClient := client.New("MyClient", "1.0", sseTransport, client.WithCapabilities(schema.ClientCapabilities{}))
These features are transport-agnostic and apply equally over HTTP, SSE, or other supported transports.
MCP supports the following Server Side methods:
initialize
- Initialize the connectionping
- Check server statusresources/list
- List available resourcesresources/read
- Read resource contentsresources/templates/list
- List resource templatesresources/subscribe
- Subscribe to resource updatesresources/unsubscribe
- Unsubscribe from resource updatesprompts/list
- List available promptsprompts/get
- Get prompt detailstools/list
- List available toolstools/call
- Call a specific toolcomplete
- Get completions from the modellogging/setLevel
- Set logging level
MCP supports the following Client Side methods:
roots/list
- List available rootssampling/createMessage
- A standardized way for servers to request LLM sampling (“completions” or “generations”) from language models via clients.
can
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache License 2.0.
Author: Adrian Witas
This project is maintained by Viant.