API Reference¶
This page provides detailed API documentation for mcpgate.
Main Module¶
mcpgate
¶
A stateless gateway that turns any OpenAPI spec into MCP tools on the fly.
Classes¶
OpenAPIMiddleware
¶
Bases: Middleware
Middleware that builds per-request MCP tools from an OpenAPI spec.
Headers extracted from each request
x-openapi-url: URL of the OpenAPI JSON specification.x-api-url: Base URL of the target API.x-cookies(optional): Cookie string forwarded to the API.
The middleware uses ContextVar to isolate the per-request
OpenAPIProvider, so concurrent requests never share state.
Providers and their HTTP clients are cached by (openapi_url, api_url)
with a configurable TTL to avoid redundant spec fetches and parsing.
Initialize the middleware with a provider cache TTL in seconds.
Source code in src/mcpgate/__init__.py
Functions¶
on_list_tools
async
¶
on_list_tools(context: MiddlewareContext[ListToolsRequest], call_next: CallNext[ListToolsRequest, Sequence[Tool]]) -> Sequence[Tool]
Prepend tools from the per-request OpenAPI provider.
Source code in src/mcpgate/__init__.py
on_call_tool
async
¶
on_call_tool(context: MiddlewareContext[CallToolRequestParams], call_next: CallNext[CallToolRequestParams, ToolResult]) -> ToolResult
Intercept tool calls destined for the per-request OpenAPI provider.
Source code in src/mcpgate/__init__.py
close
async
¶
Close all cached clients and the spec-fetching client.
Source code in src/mcpgate/__init__.py
Functions¶
create_mcp
¶
Create and return a new FastMCP instance with OpenAPI middleware.