Documentation
¶
Overview ¶
Package mcp provides a minimal Model Context Protocol server implementation for building AI tool integrations with Claude Desktop and other MCP clients.
Index ¶
- Constants
- Variables
- type Capabilities
- type ContentBlock
- type Implementation
- type InitializeParams
- type InitializeResult
- type InputSchema
- type Property
- type Request
- type Response
- type ResponseError
- type Server
- type Tool
- type ToolDefinition
- type ToolHandler
- type ToolsCallParams
- type ToolsCallResult
- type ToolsCapability
- type ToolsListResult
Constants ¶
const ( ErrorCodeInternal = -32603 ErrorCodeInvalidParams = -32602 ErrorCodeInvalidRequest = -32600 ErrorCodeMethodNotFound = -32601 ErrorCodeParse = -32700 )
JSON-RPC 2.0 standard error codes.
Variables ¶
var ( ErrMCPInvalidToolParams = errors.New("invalid tool parameters") ErrMCPNotInitialized = errors.New("server not initialized") ErrMCPToolNotFound = errors.New("tool not found") ErrMCPTransportClosed = errors.New("transport closed") )
MCP-specific errors.
Functions ¶
This section is empty.
Types ¶
type Capabilities ¶
type Capabilities struct {
Tools *ToolsCapability `json:"tools,omitempty"`
}
Capabilities represents MCP capabilities.
type ContentBlock ¶
ContentBlock represents a content block in tool results.
func NewTextContent ¶
func NewTextContent(text string) ContentBlock
NewTextContent creates a text content block.
type Implementation ¶
Implementation represents server or client implementation info.
type InitializeParams ¶
type InitializeParams struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities Capabilities `json:"capabilities"`
ClientInfo Implementation `json:"clientInfo"`
}
InitializeParams represents the parameters for the initialize method.
type InitializeResult ¶
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities Capabilities `json:"capabilities"`
ServerInfo Implementation `json:"serverInfo"`
}
InitializeResult represents the result of the initialize method.
type InputSchema ¶
type InputSchema struct {
Type string `json:"type"`
Properties map[string]Property `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
}
InputSchema represents the JSON Schema for tool input.
func NewObjectSchema ¶
func NewObjectSchema(properties map[string]Property, required []string) InputSchema
NewObjectSchema creates an object input schema.
type Property ¶
type Property struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
}
Property represents a JSON Schema property.
func NewBooleanProperty ¶
NewBooleanProperty creates a boolean property.
func NewNumberProperty ¶
NewNumberProperty creates a number property.
func NewStringProperty ¶
NewStringProperty creates a string property.
type Request ¶
type Request struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
Request represents a JSON-RPC 2.0 request.
type Response ¶
type Response struct {
Result any `json:"result,omitempty"`
Error *ResponseError `json:"error,omitempty"`
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
}
Response represents a JSON-RPC 2.0 response.
func NewErrorResponse ¶
func NewErrorResponse(id json.RawMessage, code int, message string) Response
NewErrorResponse creates a JSON-RPC 2.0 error response.
func NewResponse ¶
func NewResponse(id json.RawMessage, result any) Response
NewResponse creates a successful JSON-RPC 2.0 response.
type ResponseError ¶
type ResponseError struct {
Data any `json:"data,omitempty"`
Message string `json:"message"`
Code int `json:"code"`
}
ResponseError represents a JSON-RPC 2.0 error object.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an MCP server that handles JSON-RPC requests over STDIO.
func NewServerWithIO ¶
NewServerWithIO creates a new MCP server with custom IO (for testing).
func (*Server) RegisterTool ¶
RegisterTool registers a tool with the server.
type Tool ¶
type Tool struct {
Handler ToolHandler
Definition ToolDefinition
}
Tool represents a registered tool with its definition and handler.
func NewTool ¶
func NewTool(name, description string, schema InputSchema, handler ToolHandler) Tool
NewTool creates a new tool with the given definition and handler.
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema InputSchema `json:"inputSchema"`
}
ToolDefinition describes a tool's metadata for tools/list.
type ToolHandler ¶
type ToolHandler service.Function[ToolsCallParams, ToolsCallResult]
ToolHandler is a function that handles tool calls. It follows the cloud-native-utils Function pattern.
type ToolsCallParams ¶
type ToolsCallParams struct {
Arguments map[string]any `json:"arguments,omitempty"`
Name string `json:"name"`
}
ToolsCallParams represents the parameters for tools/call.
type ToolsCallResult ¶
type ToolsCallResult struct {
Content []ContentBlock `json:"content"`
IsError bool `json:"isError,omitempty"`
}
ToolsCallResult represents the result of tools/call.
type ToolsCapability ¶
type ToolsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
ToolsCapability represents tools capability.
type ToolsListResult ¶
type ToolsListResult struct {
Tools []ToolDefinition `json:"tools"`
}
ToolsListResult represents the result of tools/list.