Documentation
¶
Overview ¶
Package mcp is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.
Package mcp implements a Model Context Protocol server for GoFastr.
It provides tool registration, JSON-RPC 2.0 message handling, and transports for stdio and HTTP/SSE.
Index ¶
- Constants
- func RequestFromContext(ctx context.Context) (*http.Request, bool)
- func StreamSSE(w io.Writer, event, data string)
- func WithRequest(ctx context.Context, r *http.Request) context.Context
- type RPCError
- type Request
- type Response
- type Server
- func (s *Server) CallTool(ctx context.Context, name string, params map[string]any) (any, error)
- func (s *Server) HandleRequest(ctx context.Context, req Request) Response
- func (s *Server) ListTools() []Tool
- func (s *Server) RegisterTool(name, description string, inputSchema map[string]any, fn ToolHandler) error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) ServeSSE(path string) http.Handler
- func (s *Server) ServeStdio(ctx context.Context, in io.Reader, out io.Writer) error
- func (s *Server) ServerInfo() (name, version string)
- func (s *Server) SetServerInfo(name, version string)
- func (s *Server) SetServerName(name string)
- type Tool
- type ToolHandler
Constants ¶
const ( ErrMethodNotFound = -32601 ErrInvalidParams = -32602 ErrInternalError = -32603 )
JSON-RPC 2.0 standard error codes.
Variables ¶
This section is empty.
Functions ¶
func RequestFromContext ¶
RequestFromContext returns the original inbound *http.Request stashed by the transport, if any. Tool handlers that re-dispatch through an HTTP router use it to copy the caller's auth onto the internal request so session/JWT middleware re-resolves the same user instead of demoting to anonymous.
func StreamSSE ¶
StreamSSE writes a single SSE event to the writer. It is the hardened entry point for tool-result streaming and treats both arguments as untrusted:
- the event name is truncated at the first CR/LF/NUL so the caller can't terminate the "event:" field and inject a forged directive below it.
- data is collapsed to a single line (CR/LF/NUL stripped) and any occurrence of a literal SSE directive marker (e.g. "event:", "id:", "retry:", "data:") inside the payload has its colon replaced with " -" so the bytes can no longer be mistaken for — or substring-matched as — an injected directive.
func WithRequest ¶
WithRequest stashes the original inbound *http.Request in ctx so tool handlers can recover the caller's transport-level auth (Cookie / Authorization headers). The HTTP transports call this automatically; it is exported so non-HTTP callers (and tests) can populate the same slot.
Types ¶
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
RPCError represents a JSON-RPC 2.0 error object.
type Request ¶
type Request struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
Request represents a JSON-RPC 2.0 request.
type Response ¶
type Response struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id"`
Result any `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
}
Response represents a JSON-RPC 2.0 response.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an MCP server with a tool registry.
func NewServer ¶
func NewServer() *Server
NewServer creates a new MCP server with an empty tool registry.
func (*Server) CallTool ¶
CallTool is the exported form of callTool: invokes a registered tool by name with the given params. Use this when calling MCP tools in-process (tests, server-side integrations) without going through the JSON-RPC transport layer.
func (*Server) HandleRequest ¶
HandleRequest routes a JSON-RPC 2.0 request to the correct handler and returns the appropriate response.
func (*Server) ListTools ¶
ListTools returns all registered tools (handlers are nilled out for safety). This is the exported version of listTools for external consumption.
func (*Server) RegisterTool ¶
func (s *Server) RegisterTool(name, description string, inputSchema map[string]any, fn ToolHandler) error
RegisterTool adds a tool to the server's registry. Returns an error if a tool with the same name already exists.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles HTTP POST requests for MCP JSON-RPC calls. It reads a JSON-RPC request from the body and writes the response.
func (*Server) ServeSSE ¶
ServeSSE sets up an HTTP handler that supports Server-Sent Events for streaming responses. The POST endpoint at path handles JSON-RPC calls, and the GET endpoint streams responses via SSE.
func (*Server) ServeStdio ¶
ServeStdio reads JSON-RPC requests from in line-by-line and writes responses to out. It blocks until in returns EOF or ctx is cancelled.
func (*Server) ServerInfo ¶ added in v0.10.0
ServerInfo returns the name/version advertised in the MCP initialize handshake. Used by well-known discovery artifacts (e.g. an MCP server card) that mirror the handshake's serverInfo.
func (*Server) SetServerInfo ¶ added in v0.10.0
SetServerInfo overrides the name/version advertised in the MCP `initialize` handshake (serverInfo). Call before serving requests.
func (*Server) SetServerName ¶ added in v0.10.0
SetServerName overrides just the serverInfo name advertised in the MCP initialize handshake, leaving the version at its default. Used by hosts that know their app name but not a separate MCP server version.