Documentation
¶
Overview ¶
Package mcp implements the MCP client with stdio and SSE transports.
Index ¶
- type Client
- type JSONRPCError
- type JSONRPCRequest
- type JSONRPCResponse
- type ListToolsResult
- type MCPServer
- func (s *MCPServer) HandleJSONRPC(ctx context.Context, raw json.RawMessage) ([]byte, error)
- func (s *MCPServer) ListTools() []ToolDescriptor
- func (s *MCPServer) RegisterResource(res Resource)
- func (s *MCPServer) RegisterTool(name, description string, inputSchema any, handler ToolHandler)
- func (s *MCPServer) Serve(listener net.Listener) error
- func (s *MCPServer) Stop(ctx context.Context) error
- type Resource
- type SSETransport
- type ServerConfig
- type StdioTransport
- type ToolAdapter
- type ToolCallResult
- type ToolContent
- type ToolDescriptor
- type ToolHandler
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client manages an MCP server connection.
func (*Client) CallTool ¶
CallTool calls the MCP tools/call method with the given name and arguments.
type JSONRPCError ¶
JSONRPCError represents a JSON-RPC error.
type JSONRPCRequest ¶
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params any `json:"params,omitempty"`
}
JSONRPCRequest is a JSON-RPC 2.0 request.
type JSONRPCResponse ¶
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
}
JSONRPCResponse is a JSON-RPC 2.0 response.
type ListToolsResult ¶
type ListToolsResult struct {
Tools []ToolDescriptor `json:"tools"`
}
ListToolsResult is the result of a tools/list call.
type MCPServer ¶ added in v0.3.0
type MCPServer struct {
// contains filtered or unexported fields
}
MCPServer is the MCP server that exposes tools via SSE + JSON-RPC 2.0.
func NewMCPServer ¶ added in v0.3.0
func NewMCPServer(cfg ...ServerConfig) *MCPServer
NewMCPServer creates a new MCPServer with optional configuration.
func (*MCPServer) HandleJSONRPC ¶ added in v0.6.0
HandleJSONRPC parses a JSON-RPC 2.0 request from raw JSON and returns the serialized response. Supported methods: initialize, tools/list, tools/call, resources/list, prompts/get.
func (*MCPServer) ListTools ¶ added in v0.6.0
func (s *MCPServer) ListTools() []ToolDescriptor
ListTools returns a copy of all registered tool descriptors.
func (*MCPServer) RegisterResource ¶ added in v0.3.0
RegisterResource registers a static resource.
func (*MCPServer) RegisterTool ¶ added in v0.3.0
func (s *MCPServer) RegisterTool(name, description string, inputSchema any, handler ToolHandler)
RegisterTool registers an MCP tool with its handler.
type Resource ¶ added in v0.3.0
type Resource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MIMEType string `json:"mimeType,omitempty"`
}
Resource holds MCP resource metadata.
type SSETransport ¶
type SSETransport struct {
// contains filtered or unexported fields
}
SSETransport connects to an MCP server via HTTP SSE.
func NewSSETransport ¶
func NewSSETransport(url string) *SSETransport
NewSSETransport creates a new SSE transport for the given URL.
func (*SSETransport) Close ¶
func (t *SSETransport) Close() error
Close shuts down the SSE transport and releases resources.
func (*SSETransport) Connect ¶
func (t *SSETransport) Connect(ctx context.Context) error
Connect establishes the HTTP SSE connection to the MCP server.
func (*SSETransport) Receive ¶
func (t *SSETransport) Receive() ([]byte, error)
Receive reads the next event from the SSE stream.
func (*SSETransport) Send ¶
func (t *SSETransport) Send(msg []byte) error
Send posts a message to the MCP server via HTTP POST.
type ServerConfig ¶ added in v0.3.0
ServerConfig holds optional configuration for the MCPServer.
type StdioTransport ¶
type StdioTransport struct {
// contains filtered or unexported fields
}
StdioTransport connects to an MCP server via subprocess stdin/stdout.
func NewStdioTransport ¶
func NewStdioTransport(command string, args ...string) *StdioTransport
NewStdioTransport creates a new stdio transport for the given command.
func (*StdioTransport) Close ¶
func (t *StdioTransport) Close() error
Close stops the subprocess and cleans up resources.
func (*StdioTransport) Connect ¶
func (t *StdioTransport) Connect(ctx context.Context) error
Connect starts the subprocess and prepares stdin/stdout for communication.
func (*StdioTransport) Receive ¶
func (t *StdioTransport) Receive() ([]byte, error)
Receive reads the next message from the subprocess stdout.
func (*StdioTransport) Send ¶
func (t *StdioTransport) Send(msg []byte) error
Send writes a message to the subprocess stdin.
type ToolAdapter ¶
type ToolAdapter struct {
// contains filtered or unexported fields
}
ToolAdapter adapts an MCP ToolDescriptor to the tools.Tool interface.
func NewToolAdapter ¶
func NewToolAdapter(desc ToolDescriptor, client *Client) *ToolAdapter
NewToolAdapter creates a new ToolAdapter.
func (*ToolAdapter) Description ¶
func (a *ToolAdapter) Description() string
Description implements the Tool interface.
func (*ToolAdapter) Parameters ¶
func (a *ToolAdapter) Parameters() map[string]any
Parameters implements the Tool interface.
type ToolCallResult ¶
type ToolCallResult struct {
Content []ToolContent `json:"content"`
IsError bool `json:"isError"`
}
ToolCallResult is the result of a tools/call.
type ToolContent ¶
ToolContent represents content returned by an MCP tool.
type ToolDescriptor ¶
type ToolDescriptor struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema any `json:"inputSchema"`
}
ToolDescriptor represents metadata about an MCP tool from tools/list.
type ToolHandler ¶ added in v0.3.0
ToolHandler is a function that handles an MCP tool call.