mcp

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 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 (
	ParserMiddlewareType         = "mcp-parser"
	ToolFilterMiddlewareType     = "tool-filter"
	ToolCallFilterMiddlewareType = "tool-call-filter"
)

Middleware type 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 CreateParserMiddleware added in v0.2.8

func CreateParserMiddleware(_ *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateParserMiddleware factory function for MCP parser middleware

func CreateToolCallFilterMiddleware added in v0.2.8

func CreateToolCallFilterMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateToolCallFilterMiddleware factory function for tool call filter middleware

func CreateToolFilterMiddleware added in v0.2.8

func CreateToolFilterMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateToolFilterMiddleware factory function for tool filter middleware

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 NewListToolsMappingMiddleware added in v0.2.12

func NewListToolsMappingMiddleware(opts ...ToolMiddlewareOption) (types.MiddlewareFunction, error)

NewListToolsMappingMiddleware 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":[...]}}

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

func NewToolCallMappingMiddleware added in v0.2.12

func NewToolCallMappingMiddleware(opts ...ToolMiddlewareOption) (types.MiddlewareFunction, error)

NewToolCallMappingMiddleware 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 or override 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.

type ParserMiddleware added in v0.2.8

type ParserMiddleware struct{}

ParserMiddleware wraps MCP parser middleware functionality

func (*ParserMiddleware) Close added in v0.2.8

func (*ParserMiddleware) Close() error

Close cleans up any resources used by the middleware.

func (*ParserMiddleware) Handler added in v0.2.8

Handler returns the middleware function used by the proxy.

type ParserMiddlewareParams added in v0.2.8

type ParserMiddlewareParams struct {
}

ParserMiddlewareParams represents the parameters for MCP parser middleware

type ToolFilterMiddleware added in v0.2.8

type ToolFilterMiddleware struct {
	// contains filtered or unexported fields
}

ToolFilterMiddleware wraps tool filter middleware functionality

func (*ToolFilterMiddleware) Close added in v0.2.8

func (*ToolFilterMiddleware) Close() error

Close cleans up any resources used by the middleware.

func (*ToolFilterMiddleware) Handler added in v0.2.8

Handler returns the middleware function used by the proxy.

type ToolFilterMiddlewareParams added in v0.2.8

type ToolFilterMiddlewareParams struct {
	FilterTools   []string                `json:"filter_tools"`
	ToolsOverride map[string]ToolOverride `json:"tools_override"`
}

ToolFilterMiddlewareParams represents the parameters for tool filter middleware

type ToolMiddlewareOption added in v0.2.12

type ToolMiddlewareOption func(*toolMiddlewareConfig) error

ToolMiddlewareOption is a function that can be used to configure the tool middleware.

func WithToolsFilter added in v0.2.12

func WithToolsFilter(toolsFilter ...string) ToolMiddlewareOption

WithToolsFilter is a function that can be used to configure the tool middleware to use a filter list of tools.

func WithToolsOverride added in v0.2.12

func WithToolsOverride(actualName string, overrideName string, overrideDescription string) ToolMiddlewareOption

WithToolsOverride is a function that can be used to configure the tool middleware to use a map of tools to override the actual list of tools.

If an empty string is provided for either overrideName or overrideDescription, that field will be left unchanged. An error is returned if actualName is empty.

type ToolOverride added in v0.3.0

type ToolOverride struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

ToolOverride represents a tool override entry.

Directories

Path Synopsis
Package server provides the MCP (Model Context Protocol) server implementation for ToolHive.
Package server provides the MCP (Model Context Protocol) server implementation for ToolHive.

Jump to

Keyboard shortcuts

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