Repository navigation
Feature Discussion: Static Code Generation from OpenAPI Specs & Auth Auto-Wiring #3673
Closed
quotentiroler
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Context
Hi, I'm the author of mcp-generator, an open-source tool that generates production-ready FastMCP 3.x server packages from OpenAPI specs. I've been building on top of FastMCP for a while now and wanted to share some observations about gaps I've run into, in case any of these are worth considering upstream.
1. Static Code Generation from OpenAPI Specs
FastMCP currently offers two paths for working with OpenAPI specs.
FastMCP.from_openapi()creates a server dynamically at runtime, which is great for quick prototyping and simple APIs. On the other side,fastmcp generate-cliintrospects an existing MCP server and generates a CLI client from it. What's missing is the middle ground: generating editable, version-controlled Python server source files from an OpenAPI spec.The reason this matters is that teams working with larger APIs (50+ endpoints) or in regulated environments need to be able to review generated code in pull requests, run linters and type checkers against it, and modify individual tool implementations without having to subclass or monkey-patch anything. When the generated output is actual source code, teams can trace exactly what each tool does, add custom validation or response transforms, and treat the result as their own codebase rather than a black box.
A generated server package that includes
fastmcp.json,pyproject.toml, a Dockerfile, middleware scaffolding, and per-tag server modules composed viamount()gives teams a deployable starting point they can own and evolve over time. This is what mcp-generator produces today: a complete package with per-tag server modules, authentication middleware, Docker deployment files, and auto-generated tests. The main server mounts each tag module with namespacing, and thefastmcp.jsonconfig is auto-populated with features and auth settings derived from the spec.A
fastmcp generate openapi.json --output ./my_servercommand would complementfrom_openapi()the same way code generation complements runtime reflection in ORMs. One path is fast and dynamic, the other produces artifacts you can check into git and deploy with confidence.2. Auth Auto-Wiring in
from_openapi()When
from_openapi()creates tools from OpenAPI endpoints, it doesn't read the spec'ssecuritySchemesto auto-configure authentication. This means users have to manually set upOAuthProxywith authorization URLs, token URLs, and scopes, even though all of that information is already sitting right there in the spec. For someone who just wants to point FastMCP at an OpenAPI spec and get a working server, having to separately look up and configure auth endpoints is unnecessary friction.The detection logic is mechanical. If the spec contains an OAuth2
authorizationCodeflow, enableOAuthProxywith the URLs and scopes from the flow definition. If it has a Bearer scheme withbearerFormat: JWT, enable token validation. If it only definesapiKey, skip JWT validation since there's no JWT to validate. If the spec has no security definitions at all, use defaults with auth disabled. mcp-generator already implements this logic, and it works well in practice. The user only needs to provide theirclient_idandclient_secret, and everything else is extracted from the spec automatically.An
auto_auth=Trueparameter onfrom_openapi()would make this a one-line addition for users. Something likeFastMCP.from_openapi(url, auto_auth=True, client_id="...", client_secret="...")where the method readssecuritySchemes, finds the right flow, and configures the auth layer accordingly.This is probably the most self-contained of the proposals here. It could be implemented as a PR to
OpenAPIProviderorfrom_openapi()without changing any existing public API surface, since it would be opt-in via a new parameter.3. Token Forwarding
When an MCP server wraps an authenticated upstream API, there's currently no built-in mechanism to forward the MCP client's Bearer token to upstream API calls. This is a common pattern, particularly in domains like healthcare or finance where the upstream API requires OAuth2 tokens and the MCP server is essentially a thin proxy that needs to pass the client's authorization through to the backend.
The simplest version of this would be a declarative option like
forward_auth=Trueonfrom_openapi(), handling the case where the same token is valid for both the MCP server and the upstream API. The server would extract the Bearer token from the incoming MCP request and attach it to the outgoing HTTP call to the upstream API. No token transformation, just pass-through.For more complex scenarios involving token exchange (RFC 8693), something like a
token_exchangeparameter with the exchange endpoint and target audience would cover the gap. This is common in multi-tenant setups where the MCP server's audience differs from the upstream API's audience, so the token needs to be exchanged before forwarding.This would need some design discussion around where tokens live in the MCP request context and how they flow through the tool execution pipeline, but the use case is real and comes up frequently when wrapping existing authenticated APIs.
4. Tool Lifecycle Hooks
FastMCP 3.x supports MCP protocol features like progress reporting, elicitation (requesting additional input from the client mid-tool), and sampling (asking the LLM to generate content). These are powerful features, but wiring them into individual tools currently requires writing boilerplate code that isn't particularly discoverable. A developer has to know these features exist and then manually implement the context manager patterns for each tool.
mcp-generator handles this by generating per-tool wrappers based on configuration. When
progress=Trueis set, it wraps the tool in a progress context manager that reports execution status back to the client. Whenelicit_missing=Trueis set, it checks for None optional parameters and prompts the client to fill them before proceeding. Whensample_on_error=Trueis set, it catches exceptions and sends the error context to the LLM via sampling to get a suggested fix or explanation.These are fundamentally per-tool concerns that don't belong in middleware, since different tools in the same server may need different combinations of these features. Making them available as decorator-level options (e.g.
@mcp.tool(progress=True, elicit_missing=True)) would make them consistent across tools and easy to discover through autocomplete and documentation. It would also lower the barrier for developers who want to use these MCP protocol features but don't want to write the boilerplate themselves.Happy to discuss any of these further or adjust the scope. I'm open to contributing PRs for any of them, and mcp-generator already implements all four in various forms, so there's working reference code to draw from. Thanks for building FastMCP, it's been a great foundation to build on.
All reactions