Documentation
¶
Index ¶
- Variables
- func AddTool[In, Out any](r *Runtime, t *mcp.Tool, h mcp.ToolHandlerFor[In, Out])
- func AuthorizationServerMetadataHandler(tokenPath string) http.Handler
- func ClientIDFromContext(ctx context.Context) string
- func ContextWithClientID(ctx context.Context, clientID string) context.Context
- func LogAuth(ctx context.Context, logger *slog.Logger, event string, success bool, ...)
- func LogEvent(ctx context.Context, logger *slog.Logger, level slog.Level, event string, ...)
- func LogToolCall(ctx context.Context, logger *slog.Logger, toolName string, success bool, ...)
- func LoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func LoggingMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func ProtectedResourceMetadataHandler(mcpPath string) http.Handler
- type ExternalAuthOptions
- type HTTPServerOptions
- type HTTPServerResult
- type NgrokOptions
- type OAuth2Credentials
- type OAuth2Options
- type OAuthCredentials
- type OAuthOptions
- type Options
- type PolicyAuthorizer
- type PolicyRule
- type PromptHandler
- type RateLimiter
- type RateLimiterConfig
- type ResourceHandler
- type Runtime
- func (r *Runtime) AddPrompt(p *mcp.Prompt, h mcp.PromptHandler)
- func (r *Runtime) AddResource(res *mcp.Resource, h mcp.ResourceHandler)
- func (r *Runtime) AddResourceTemplate(t *mcp.ResourceTemplate, h mcp.ResourceHandler)
- func (r *Runtime) AddToolHandler(t *mcp.Tool, h mcp.ToolHandler)
- func (r *Runtime) CallTool(ctx context.Context, name string, args any) (*mcp.CallToolResult, error)
- func (r *Runtime) Connect(ctx context.Context, transport mcp.Transport) (*mcp.ServerSession, error)
- func (r *Runtime) GetPrompt(ctx context.Context, name string, args map[string]string) (*mcp.GetPromptResult, error)
- func (r *Runtime) HasPrompt(name string) bool
- func (r *Runtime) HasResource(uri string) bool
- func (r *Runtime) HasTool(name string) bool
- func (r *Runtime) Implementation() *mcp.Implementation
- func (r *Runtime) InMemorySession(ctx context.Context) (*mcp.ServerSession, *mcp.ClientSession, error)
- func (r *Runtime) ListPrompts() []*mcp.Prompt
- func (r *Runtime) ListResources() []*mcp.Resource
- func (r *Runtime) ListTools() []*mcp.Tool
- func (r *Runtime) MCPServer() *mcp.Server
- func (r *Runtime) PromptCount() int
- func (r *Runtime) ReadResource(ctx context.Context, uri string) (*mcp.ReadResourceResult, error)
- func (r *Runtime) RegisterSkill(s skill.Skill)
- func (r *Runtime) RegisterSkillWithPrefix(s skill.Skill)
- func (r *Runtime) RemovePrompts(names ...string)
- func (r *Runtime) RemoveResourceTemplates(uriTemplates ...string)
- func (r *Runtime) RemoveResources(uris ...string)
- func (r *Runtime) RemoveTools(names ...string)
- func (r *Runtime) ResourceCount() int
- func (r *Runtime) SSEHandler(opts *mcp.SSEOptions) http.Handler
- func (r *Runtime) Serve(ctx context.Context, transport mcp.Transport) error
- func (r *Runtime) ServeHTTP(ctx context.Context, opts *HTTPServerOptions) (*HTTPServerResult, error)
- func (r *Runtime) ServeIO(ctx context.Context, reader io.ReadCloser, writer io.WriteCloser) error
- func (r *Runtime) ServeStdio(ctx context.Context) error
- func (r *Runtime) StreamableHTTPHandler(opts *mcp.StreamableHTTPOptions) http.Handler
- func (r *Runtime) ToolCount() int
- type ToolAccessDeniedError
- type ToolAuthorizer
- type ToolAuthorizerConfig
- type ToolHandler
- type ToolHandlerFor
Constants ¶
This section is empty.
Variables ¶
var ErrPromptNotFound = errors.New("prompt not found")
ErrPromptNotFound is returned when attempting to get a prompt that doesn't exist.
var ErrResourceNotFound = errors.New("resource not found")
ErrResourceNotFound is returned when attempting to read a resource that doesn't exist.
var ErrToolNotFound = errors.New("tool not found")
ErrToolNotFound is returned when attempting to call a tool that doesn't exist.
Functions ¶
func AddTool ¶
AddTool adds a typed tool to the runtime with automatic schema inference.
This mirrors mcp.AddTool from the MCP SDK. The generic type parameters In and Out are used to automatically generate JSON schemas for the tool's input and output if not already specified in the Tool struct.
The In type provides the default input schema (must be a struct or map). The Out type provides the default output schema (use 'any' to omit).
Example:
type AddInput struct {
A int `json:"a" jsonschema:"first number to add"`
B int `json:"b" jsonschema:"second number to add"`
}
type AddOutput struct {
Sum int `json:"sum"`
}
mcpkit.AddTool(rt, &mcp.Tool{
Name: "add",
Description: "Add two numbers",
}, func(ctx context.Context, req *mcp.CallToolRequest, in AddInput) (*mcp.CallToolResult, AddOutput, error) {
return nil, AddOutput{Sum: in.A + in.B}, nil
})
func AuthorizationServerMetadataHandler ¶
AuthorizationServerMetadataHandler returns an http.Handler for the OAuth 2.0 Authorization Server Metadata endpoint (RFC 8414). This should be mounted at /.well-known/oauth-authorization-server The tokenPath is the path to the token endpoint (e.g., "/oauth/token").
func ClientIDFromContext ¶ added in v0.11.0
ClientIDFromContext retrieves the client ID from context.
func ContextWithClientID ¶ added in v0.11.0
ContextWithClientID returns a context with the client ID stored.
func LogAuth ¶ added in v0.11.0
func LogAuth(ctx context.Context, logger *slog.Logger, event string, success bool, clientID string, err error)
LogAuth logs an authentication event.
func LogEvent ¶ added in v0.11.0
func LogEvent(ctx context.Context, logger *slog.Logger, level slog.Level, event string, attrs ...any)
LogEvent logs a structured event with the given level.
func LogToolCall ¶ added in v0.11.0
func LogToolCall(ctx context.Context, logger *slog.Logger, toolName string, success bool, duration time.Duration, err error)
LogToolCall logs a tool invocation.
func LoggerMiddleware ¶ added in v0.11.0
LoggerMiddleware injects a logger into the request context.
This allows handlers to retrieve a request-scoped logger via slogutil.LoggerFromContext(ctx) with request-specific attributes already attached.
func LoggingMiddleware ¶ added in v0.11.0
LoggingMiddleware returns middleware that logs HTTP requests.
It logs request start (at DEBUG level) and completion (at INFO level), including method, path, status code, and duration.
The logger is retrieved from context if available (via slogutil.LoggerFromContext), falling back to the provided logger or slog.Default().
func ProtectedResourceMetadataHandler ¶
ProtectedResourceMetadataHandler returns an http.Handler for the OAuth 2.0 Protected Resource Metadata endpoint (RFC 9728). This should be mounted at /.well-known/oauth-protected-resource The mcpPath is the path to the MCP endpoint (e.g., "/mcp").
Types ¶
type ExternalAuthOptions ¶ added in v0.12.0
type ExternalAuthOptions struct {
// Verifier validates incoming bearer tokens. Required.
Verifier oauth2.TokenVerifier
// AuthorizationServers lists the issuer URLs of the external
// authorization servers that issue tokens for this resource, advertised
// via RFC 9728 protected resource metadata. Required.
AuthorizationServers []string
// Resource is the protected resource identifier (and typically the
// expected token audience). Defaults to the server's base URL plus the
// MCP path.
Resource string
// ScopesSupported optionally lists scopes advertised in the protected
// resource metadata.
ScopesSupported []string
}
ExternalAuthOptions configures resource-server authentication against externally-issued tokens (see HTTPServerOptions.ExternalAuth).
type HTTPServerOptions ¶
type HTTPServerOptions struct {
// Addr is the local address to listen on (e.g., ":8080").
// Required when Ngrok is nil. When Ngrok is configured, this is optional
// and defaults to a random available port.
Addr string
// Path is the HTTP path for the MCP endpoint. Defaults to "/mcp".
Path string
// ReadHeaderTimeout is the timeout for reading request headers.
// Defaults to 10 seconds.
ReadHeaderTimeout time.Duration
// Ngrok configures optional ngrok tunneling. When set, the server
// is exposed via ngrok and the PublicURL in the result will be populated.
Ngrok *NgrokOptions
// StreamableHTTPOptions are passed to the MCP StreamableHTTP handler.
StreamableHTTPOptions *mcp.StreamableHTTPOptions
// OAuth configures simple OAuth 2.0 client credentials authentication.
// When set, the /mcp endpoint requires a Bearer token and a token
// endpoint is exposed at /oauth/token (or OAuth.TokenPath if set).
// Deprecated: Use OAuth2 for full OAuth 2.1 with PKCE support (required by ChatGPT.com).
OAuth *OAuthOptions
// OAuth2 configures full OAuth 2.1 Authorization Code + PKCE authentication.
// This is required for ChatGPT.com and other clients that need DCR and PKCE.
// When set, the following endpoints are exposed:
// - /oauth/authorize - Authorization endpoint with login page
// - /oauth/token - Token endpoint
// - /oauth/register - Dynamic Client Registration
// - /.well-known/oauth-authorization-server - Metadata
// - /.well-known/oauth-protected-resource - Resource metadata
OAuth2 *OAuth2Options
// ExternalAuth makes this server a pure OAuth resource server: bearer
// tokens are validated by an externally-supplied verifier (e.g. JWTs
// issued by an enterprise IdP or authorization server in an ID-JAG /
// MCP Enterprise-Managed Authorization deployment) and no local
// authorization server endpoints are mounted. Only
// /.well-known/oauth-protected-resource is exposed, advertising the
// external authorization servers. Mutually exclusive with OAuth and
// OAuth2.
ExternalAuth *ExternalAuthOptions
// OnReady is called when the server is ready to accept connections,
// before ServeHTTP blocks. This is useful for logging the server URL.
OnReady func(result *HTTPServerResult)
}
HTTPServerOptions configures HTTP-based serving.
type HTTPServerResult ¶
type HTTPServerResult struct {
// LocalAddr is the local address the server is listening on (e.g., "localhost:8080").
LocalAddr string
// LocalURL is the full local URL including path (e.g., "http://localhost:8080/mcp").
LocalURL string
// PublicURL is the ngrok public URL including path, if ngrok is enabled.
// Empty string if ngrok is not configured.
PublicURL string
// OAuth contains the OAuth credentials if OAuth (client_credentials) is enabled.
// Nil if OAuth is not configured.
// Deprecated: Use OAuth2 for full OAuth 2.1 support.
OAuth *OAuthCredentials
// OAuth2 contains the OAuth 2.1 server information if OAuth2 is enabled.
// Nil if OAuth2 is not configured.
OAuth2 *OAuth2Credentials
}
HTTPServerResult contains information about the running HTTP server.
type NgrokOptions ¶
type NgrokOptions struct {
// Authtoken is the ngrok authentication token.
// If empty, uses the NGROK_AUTHTOKEN environment variable.
Authtoken string
// Domain is an optional custom ngrok domain (e.g., "myapp.ngrok.io").
// Requires a paid ngrok plan.
Domain string
}
NgrokOptions configures ngrok tunneling.
type OAuth2Credentials ¶
type OAuth2Credentials struct {
// ClientID is the pre-registered client ID.
ClientID string
// ClientSecret is the pre-registered client secret.
ClientSecret string
// AuthorizationEndpoint is the authorization URL.
AuthorizationEndpoint string
// TokenEndpoint is the token URL.
TokenEndpoint string
// RegistrationEndpoint is the DCR URL.
RegistrationEndpoint string
// Users is the map of configured users (for display/logging).
Users []string
}
OAuth2Credentials contains the OAuth 2.1 server information.
type OAuth2Options ¶
type OAuth2Options struct {
// Users is a map of username to password for authentication.
// At least one user must be configured.
Users map[string]string
// ClientID is the pre-registered OAuth client ID.
// If empty, one will be auto-generated.
ClientID string
// ClientSecret is the pre-registered OAuth client secret.
// If empty, one will be auto-generated.
ClientSecret string
// RedirectURIs is the list of allowed redirect URIs for the pre-registered client.
// Defaults to allowing any URI (for flexibility with ChatGPT.com).
RedirectURIs []string
// AccessTokenExpiry is how long access tokens are valid.
// Defaults to 1 hour.
AccessTokenExpiry time.Duration
// RefreshTokenExpiry is how long refresh tokens are valid.
// Defaults to 24 hours. Set to 0 to disable refresh tokens.
RefreshTokenExpiry time.Duration
// AllowedScopes is the list of scopes this server supports.
// If empty, no scope validation is performed.
AllowedScopes []string
// LoginPageTemplate is custom HTML for the login page.
// If empty, a default login page is used.
LoginPageTemplate string
// Debug enables verbose logging for OAuth operations.
Debug bool
}
OAuth2Options configures OAuth 2.1 Authorization Code + PKCE authentication.
type OAuthCredentials ¶
type OAuthCredentials struct {
// ClientID is the OAuth client ID (provided or auto-generated).
ClientID string
// ClientSecret is the OAuth client secret (provided or auto-generated).
ClientSecret string
// TokenEndpoint is the full URL of the token endpoint.
TokenEndpoint string
}
OAuthCredentials contains the OAuth credentials for the server. This is returned in HTTPServerResult when OAuth is enabled.
type OAuthOptions ¶
type OAuthOptions struct {
// ClientID is the OAuth client ID. If empty, one will be auto-generated.
ClientID string
// ClientSecret is the OAuth client secret. If empty, one will be auto-generated.
ClientSecret string
// TokenExpiry is how long access tokens are valid. Defaults to 1 hour.
TokenExpiry time.Duration
// TokenPath is the path for the token endpoint. Defaults to "/oauth/token".
TokenPath string
}
OAuthOptions configures OAuth 2.0 client credentials grant authentication.
type Options ¶
type Options struct {
// Logger for runtime activity. If nil, a default logger is used.
Logger *slog.Logger
// ServerOptions are passed directly to the underlying mcp.Server.
ServerOptions *mcp.ServerOptions
// Registry for auto-registration of skills. If set, skills registered
// with RegisterSkill will also be registered with this registry.
Registry registry.Registry
}
Options configures a Runtime.
type PolicyAuthorizer ¶ added in v0.11.0
type PolicyAuthorizer struct {
// contains filtered or unexported fields
}
PolicyAuthorizer implements authorization based on rules.
func NewPolicyAuthorizer ¶ added in v0.11.0
func NewPolicyAuthorizer(rules []PolicyRule) *PolicyAuthorizer
NewPolicyAuthorizer creates a policy-based authorizer. Rules are evaluated in order; first match wins. If no rules match, access is denied by default.
func (*PolicyAuthorizer) Authorize ¶ added in v0.11.0
func (pa *PolicyAuthorizer) Authorize(_ context.Context, clientID, toolName string) bool
Authorize checks if the client can access the tool.
func (*PolicyAuthorizer) AuthorizerFunc ¶ added in v0.11.0
AuthorizerFunc returns an authorizer function for use with ToolAuthorizerConfig.
type PolicyRule ¶ added in v0.11.0
type PolicyRule struct {
// ClientID pattern to match (* for all).
ClientID string
// Tools is the list of tool patterns to match (* for all).
Tools []string
// Allow determines if matching requests are allowed.
Allow bool
}
PolicyRule defines an authorization rule.
type PromptHandler ¶
type PromptHandler = mcp.PromptHandler
PromptHandler is an alias for the MCP SDK's prompt handler.
type RateLimiter ¶ added in v0.11.0
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter stores per-client rate limiters.
func NewRateLimiter ¶ added in v0.11.0
func NewRateLimiter(cfg *RateLimiterConfig) *RateLimiter
NewRateLimiter creates a new rate limiter with the given configuration.
func (*RateLimiter) Middleware ¶ added in v0.11.0
func (rl *RateLimiter) Middleware() func(http.Handler) http.Handler
Middleware returns rate limiting middleware.
type RateLimiterConfig ¶ added in v0.11.0
type RateLimiterConfig struct {
// RequestsPerSecond is the maximum requests per second per client.
// Zero or negative means no limit.
RequestsPerSecond float64
// BurstSize is the maximum burst size (token bucket capacity).
// Defaults to 10 if not specified.
BurstSize int
// KeyFunc extracts the client key from the request.
// Defaults to using the client's IP address.
KeyFunc func(r *http.Request) string
// OnLimited is called when a request is rate limited.
// If nil, returns 429 Too Many Requests.
OnLimited func(w http.ResponseWriter, r *http.Request)
// Logger for rate limit events.
Logger *slog.Logger
}
RateLimiterConfig configures rate limiting.
type ResourceHandler ¶
type ResourceHandler = mcp.ResourceHandler
ResourceHandler is an alias for the MCP SDK's resource handler.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime is the core type for mcpkit. It wraps an MCP Server and provides both library-mode direct invocation and transport-based MCP server capabilities.
A Runtime should be created with New and configured with tools, prompts, and resources before use.
func New ¶
func New(impl *mcp.Implementation, opts *Options) *Runtime
New creates a new Runtime with the given implementation info and options.
The implementation parameter must not be nil and describes the server identity (name, version, etc.) that will be reported to MCP clients.
The options parameter may be nil to use default options.
func (*Runtime) AddPrompt ¶
func (r *Runtime) AddPrompt(p *mcp.Prompt, h mcp.PromptHandler)
AddPrompt adds a prompt to the runtime.
The prompt handler is called when clients request the prompt via prompts/get. In library mode, it can be invoked directly via Runtime.GetPrompt.
Example:
rt.AddPrompt(&mcp.Prompt{
Name: "summarize",
Description: "Summarize the given text",
Arguments: []*mcp.PromptArgument{
{Name: "text", Description: "Text to summarize", Required: true},
},
}, func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
text := req.Params.Arguments["text"]
return &mcp.GetPromptResult{
Messages: []*mcp.PromptMessage{
{Role: "user", Content: &mcp.TextContent{
Text: fmt.Sprintf("Please summarize: %s", text),
}},
},
}, nil
})
func (*Runtime) AddResource ¶
func (r *Runtime) AddResource(res *mcp.Resource, h mcp.ResourceHandler)
AddResource adds a resource to the runtime.
The resource handler is called when clients request the resource via resources/read. In library mode, it can be invoked directly via Runtime.ReadResource.
Example:
rt.AddResource(&mcp.Resource{
URI: "config://app/settings",
Name: "settings",
Description: "Application settings",
MIMEType: "application/json",
}, func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
return &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
URI: req.Params.URI,
Text: `{"debug": true}`,
}},
}, nil
})
func (*Runtime) AddResourceTemplate ¶
func (r *Runtime) AddResourceTemplate(t *mcp.ResourceTemplate, h mcp.ResourceHandler)
AddResourceTemplate adds a resource template to the runtime.
Resource templates allow dynamic resource URIs using URI template syntax (RFC 6570). The handler is called for any URI matching the template.
Note: Resource templates are registered with the MCP server but not currently supported in library-mode dispatch. Use Runtime.MCPServer for full resource template support.
func (*Runtime) AddToolHandler ¶
func (r *Runtime) AddToolHandler(t *mcp.Tool, h mcp.ToolHandler)
AddToolHandler adds a tool with a low-level handler to the runtime.
This is the low-level API that mirrors mcp.Server.AddTool. It does not perform automatic input validation or output schema generation.
The tool's InputSchema must be non-nil and have type "object". See mcp.Server.AddTool for full documentation on requirements.
Most users should use the generic AddTool function instead.
func (*Runtime) CallTool ¶
CallTool invokes a tool by name with the given arguments.
This is the library-mode entry point for tool invocation. It bypasses MCP JSON-RPC transport and directly invokes the tool handler.
The args parameter should be a map[string]any or a struct that can be marshaled to JSON matching the tool's input schema.
Returns ErrToolNotFound if no tool with the given name exists.
func (*Runtime) Connect ¶
Connect creates a session for a single connection.
Unlike Runtime.ServeStdio which runs a blocking loop, Connect returns immediately with a session that can be used to await client termination or manage the connection lifecycle.
This is useful for HTTP-based transports or when managing multiple concurrent sessions.
func (*Runtime) GetPrompt ¶
func (r *Runtime) GetPrompt(ctx context.Context, name string, args map[string]string) (*mcp.GetPromptResult, error)
GetPrompt retrieves a prompt by name with the given arguments.
This is the library-mode entry point for prompt retrieval. It bypasses MCP JSON-RPC transport and directly invokes the prompt handler.
Returns ErrPromptNotFound if no prompt with the given name exists.
func (*Runtime) HasResource ¶
HasResource reports whether a resource with the given URI is registered.
func (*Runtime) Implementation ¶
func (r *Runtime) Implementation() *mcp.Implementation
Implementation returns the server's implementation info.
func (*Runtime) InMemorySession ¶
func (r *Runtime) InMemorySession(ctx context.Context) (*mcp.ServerSession, *mcp.ClientSession, error)
InMemorySession creates an in-memory client-server session pair.
This is useful for testing or for scenarios where you want MCP semantics (including JSON-RPC serialization) but don't need network transport.
Returns the server session and client session. The caller should close the client session when done, which will also terminate the server session.
Example:
serverSession, clientSession, err := rt.InMemorySession(ctx)
if err != nil {
log.Fatal(err)
}
defer clientSession.Close()
// Use clientSession to call tools via MCP protocol
result, err := clientSession.CallTool(ctx, &mcp.CallToolParams{Name: "add", Arguments: map[string]any{"a": 1, "b": 2}})
func (*Runtime) ListPrompts ¶
ListPrompts returns all registered prompts.
func (*Runtime) ListResources ¶
ListResources returns all registered resources.
func (*Runtime) MCPServer ¶
MCPServer returns the underlying mcp.Server for advanced use cases.
This is an escape hatch for scenarios where direct access to the MCP SDK server is needed, such as plugging into existing MCP infrastructure or accessing features not yet exposed by mcpkit.
Use with caution: modifications to the returned server may not be reflected in mcpkit's library-mode dispatch.
func (*Runtime) PromptCount ¶
PromptCount returns the number of registered prompts.
func (*Runtime) ReadResource ¶
ReadResource reads a resource by URI.
This is the library-mode entry point for resource reading. It bypasses MCP JSON-RPC transport and directly invokes the resource handler.
Returns ErrResourceNotFound if no resource with the given URI exists.
func (*Runtime) RegisterSkill ¶
RegisterSkill registers a skill and all its tools with the runtime.
Each tool from the skill is converted to an MCP tool and registered. The skill's Init method is NOT called - callers should initialize skills before registration if needed.
If a Registry was configured via Options, the skill is also registered with the registry for discovery by other components.
Tool names are registered as-is (no skill prefix). If you need namespacing, include it in the tool names within the skill.
Example:
weatherSkill := &WeatherSkill{apiKey: os.Getenv("WEATHER_API_KEY")}
if err := weatherSkill.Init(ctx); err != nil {
return err
}
rt.RegisterSkill(weatherSkill)
func (*Runtime) RegisterSkillWithPrefix ¶
RegisterSkillWithPrefix registers a skill with tool name prefixing.
Each tool name is prefixed with "skillname_" to avoid conflicts. For example, a skill named "weather" with tool "get_forecast" would register as "weather_get_forecast".
If a Registry was configured via Options, the skill is also registered with the registry for discovery by other components.
func (*Runtime) RemovePrompts ¶
RemovePrompts removes prompts with the given names from the runtime.
func (*Runtime) RemoveResourceTemplates ¶
RemoveResourceTemplates removes resource templates with the given URI templates.
func (*Runtime) RemoveResources ¶
RemoveResources removes resources with the given URIs from the runtime.
func (*Runtime) RemoveTools ¶
RemoveTools removes tools with the given names from the runtime.
func (*Runtime) ResourceCount ¶
ResourceCount returns the number of registered resources.
func (*Runtime) SSEHandler ¶
func (r *Runtime) SSEHandler(opts *mcp.SSEOptions) http.Handler
SSEHandler returns an http.Handler for the legacy SSE transport.
This is provided for backwards compatibility with older MCP clients. New implementations should prefer Runtime.StreamableHTTPHandler.
func (*Runtime) Serve ¶
Serve runs the runtime with a custom MCP transport.
This is the most flexible option, allowing any transport that implements the mcp.Transport interface.
func (*Runtime) ServeHTTP ¶
func (r *Runtime) ServeHTTP(ctx context.Context, opts *HTTPServerOptions) (*HTTPServerResult, error)
ServeHTTP starts an HTTP server for the MCP runtime.
When opts.Ngrok is configured, the server is exposed via ngrok tunnel and the returned result includes the public URL.
ServeHTTP blocks until the context is cancelled, at which point it performs a graceful shutdown.
Example without ngrok:
result, err := rt.ServeHTTP(ctx, &mcpkit.HTTPServerOptions{
Addr: ":8080",
})
if err != nil {
log.Fatal(err)
}
log.Printf("MCP server running at %s", result.LocalURL)
Example with ngrok:
result, err := rt.ServeHTTP(ctx, &mcpkit.HTTPServerOptions{
Ngrok: &mcpkit.NgrokOptions{
Authtoken: os.Getenv("NGROK_AUTHTOKEN"),
},
})
if err != nil {
log.Fatal(err)
}
log.Printf("MCP server running at %s", result.PublicURL)
func (*Runtime) ServeIO ¶
func (r *Runtime) ServeIO(ctx context.Context, reader io.ReadCloser, writer io.WriteCloser) error
ServeIO runs the runtime as an MCP server over custom IO streams.
This is useful for testing or when you need to control the IO streams directly rather than using stdin/stdout.
func (*Runtime) ServeStdio ¶
ServeStdio runs the runtime as an MCP server over stdio transport.
This is the standard way to run an MCP server as a subprocess. The server communicates with the client via stdin/stdout using newline-delimited JSON.
ServeStdio blocks until the client terminates the connection or the context is cancelled.
Example:
func main() {
rt := mcpkit.New(&mcp.Implementation{Name: "my-server", Version: "v1.0.0"}, nil)
// ... register tools ...
if err := rt.ServeStdio(context.Background()); err != nil {
log.Fatal(err)
}
}
func (*Runtime) StreamableHTTPHandler ¶
func (r *Runtime) StreamableHTTPHandler(opts *mcp.StreamableHTTPOptions) http.Handler
StreamableHTTPHandler returns an http.Handler for MCP's Streamable HTTP transport.
This enables serving MCP over HTTP using Server-Sent Events (SSE) for server-to-client messages. The handler can be mounted on any HTTP server.
Example:
rt := mcpkit.New(&mcp.Implementation{Name: "my-server", Version: "v1.0.0"}, nil)
// ... register tools ...
http.Handle("/mcp", rt.StreamableHTTPHandler(nil))
http.ListenAndServe(":8080", nil)
type ToolAccessDeniedError ¶ added in v0.11.0
ToolAccessDeniedError is returned when tool access is denied.
func (*ToolAccessDeniedError) Error ¶ added in v0.11.0
func (e *ToolAccessDeniedError) Error() string
type ToolAuthorizer ¶ added in v0.11.0
type ToolAuthorizer struct {
// contains filtered or unexported fields
}
ToolAuthorizer provides per-tool access control.
func NewToolAuthorizer ¶ added in v0.11.0
func NewToolAuthorizer(cfg *ToolAuthorizerConfig) *ToolAuthorizer
NewToolAuthorizer creates a new tool authorizer.
func (*ToolAuthorizer) CheckAccess ¶ added in v0.11.0
func (ta *ToolAuthorizer) CheckAccess(ctx context.Context, toolName string) error
CheckAccess verifies if the client can access the given tool. Returns nil if allowed, error if denied.
func (*ToolAuthorizer) Middleware ¶ added in v0.11.0
func (ta *ToolAuthorizer) Middleware() func(http.Handler) http.Handler
Middleware returns HTTP middleware that checks tool access for MCP tool calls. This middleware inspects the request body to extract the tool name from MCP CallTool requests.
func (*ToolAuthorizer) WrapToolHandler ¶ added in v0.11.0
func (ta *ToolAuthorizer) WrapToolHandler(toolName string, handler http.HandlerFunc) http.HandlerFunc
WrapToolHandler wraps a tool handler with authorization checks. This is the recommended way to add per-tool authorization.
type ToolAuthorizerConfig ¶ added in v0.11.0
type ToolAuthorizerConfig struct {
// Authorizer checks if a client can access a tool.
// Returns true if access is allowed.
// The clientID is extracted from the request context (set by auth middleware).
Authorizer func(ctx context.Context, clientID, toolName string) bool
// ClientIDFromContext extracts the client ID from the request context.
// Defaults to looking for "client_id" in context values.
ClientIDFromContext func(ctx context.Context) string
// OnDenied is called when access is denied.
// If nil, returns 403 Forbidden.
OnDenied func(w http.ResponseWriter, r *http.Request, toolName string)
// Logger for authorization events.
Logger *slog.Logger
}
ToolAuthorizerConfig configures per-tool authorization.
type ToolHandler ¶
type ToolHandler = mcp.ToolHandler
ToolHandler is an alias for the MCP SDK's low-level tool handler.
type ToolHandlerFor ¶
type ToolHandlerFor[In, Out any] = mcp.ToolHandlerFor[In, Out]
ToolHandlerFor is an alias for the MCP SDK's typed tool handler. It provides automatic input/output schema inference and validation.