mcp

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mcp provides MCP (Model Context Protocol) parsing utilities and middleware.

Index

Constants

View Source
const (
	// MCPRequestContextKey is the context key for storing parsed MCP request data.
	MCPRequestContextKey contextKey = "mcp_request"
)

Variables

This section is empty.

Functions

func GetMCPArguments

func GetMCPArguments(ctx context.Context) map[string]interface{}

GetMCPArguments is a convenience function to get the MCP arguments from the context.

func GetMCPMethod

func GetMCPMethod(ctx context.Context) string

GetMCPMethod is a convenience function to get the MCP method from the context.

func GetMCPResourceID

func GetMCPResourceID(ctx context.Context) string

GetMCPResourceID is a convenience function to get the MCP resource ID from the context.

func NewToolCallFilterMiddleware added in v0.2.1

func NewToolCallFilterMiddleware(filterTools []string) (types.MiddlewareFunction, error)

NewToolCallFilterMiddleware creates an HTTP middleware that parses tool call requests and filters out tools that are not in the filter list.

The middleware looks for JSON-RPC messages with: - method: tool/call - params: {"name": "tool_name"}

This middleware is designed to be used ONLY when tool filtering is enabled, and expects the list of tools to be "correct" (i.e. not empty and not containing nonexisting tools).

func NewToolFilterMiddleware added in v0.2.1

func NewToolFilterMiddleware(filterTools []string) (types.MiddlewareFunction, error)

NewToolFilterMiddleware creates an HTTP middleware that parses SSE responses and plain JSON objects to extract tool names from JSON-RPC messages containing tool lists or tool calls.

The middleware looks for SSE events with: - event: message - data: {"jsonrpc":"2.0","id":X,"result":{"tools":[...]}}

When it finds such messages, it prints the name of each tool in the list. If filterTools is provided, only tools in that list will be logged. If filterTools is nil or empty, all tools will be logged.

This middleware is designed to be used ONLY when tool filtering is enabled, and expects the list of tools to be "correct" (i.e. not empty and not containing nonexisting tools).

func ParsingMiddleware

func ParsingMiddleware(next http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL