Documentation
¶
Overview ¶
Package mcp provides MCP (Model Context Protocol) parsing utilities and middleware.
Index ¶
Constants ¶
const (
// MCPRequestContextKey is the context key for storing parsed MCP request data.
MCPRequestContextKey contextKey = "mcp_request"
)
Variables ¶
This section is empty.
Functions ¶
func GetMCPArguments ¶
GetMCPArguments is a convenience function to get the MCP arguments from the context.
func GetMCPMethod ¶
GetMCPMethod is a convenience function to get the MCP method from the context.
func GetMCPResourceID ¶
GetMCPResourceID is a convenience function to get the MCP resource ID from the context.
func ParsingMiddleware ¶
ParsingMiddleware creates an HTTP middleware that parses MCP JSON-RPC requests and stores the parsed information in the request context for use by downstream middleware (authorization, audit, etc.).
The middleware: 1. Checks if the request should be parsed (POST with JSON content to MCP endpoints) 2. Reads and parses the JSON-RPC message 3. Extracts method, parameters, and resource information 4. Stores the parsed data in request context 5. Restores the request body for downstream handlers
Example usage:
middlewares := []types.Middleware{ authMiddleware, // Authentication first mcp.ParsingMiddleware, // MCP parsing after auth authzMiddleware, // Authorization uses parsed data auditMiddleware, // Audit uses parsed data }
Types ¶
type ParsedMCPRequest ¶
type ParsedMCPRequest struct { // Method is the MCP method name (e.g., "tools/call", "resources/read") Method string // ID is the JSON-RPC request ID ID interface{} // Params contains the raw JSON parameters Params json.RawMessage // ResourceID is the extracted resource identifier (tool name, resource URI, etc.) ResourceID string // Arguments contains the extracted arguments for the operation Arguments map[string]interface{} // IsRequest indicates if this is a JSON-RPC request (vs response or notification) IsRequest bool // IsBatch indicates if this is a batch request IsBatch bool }
ParsedMCPRequest contains the parsed MCP request information.
func GetParsedMCPRequest ¶
func GetParsedMCPRequest(ctx context.Context) *ParsedMCPRequest
GetParsedMCPRequest retrieves the parsed MCP request from the request context. Returns nil if no parsed request is available.