mcp

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: Apache-2.0 Imports: 8 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 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