Learn how to build MCP servers from your OpenAPI documents using Speakeasy for production-ready servers with extensive customization options.
Getting started with Speakeasy
Use the following steps to build an MCP server using Speakeasy.
Install the Speakeasy CLI with the following commands:
# Homebrew (macOS)brew install speakeasy-api/tap/speakeasy# Script Installation (macOS and Linux)curl -fsSL https://go.speakeasy.com/cli-install.sh | sh
Run the Speakeasy quickstart command:
speakeasy quickstart --mcp
Indicate whether you plan to deploy your server on Cloudflare.
Cloudflare is a popular choice for hosting MCP servers. If selected, a Cloudflare Worker config file is generated with your MCP server code, making it easy to deploy your server to a Cloudflare Worker.
These steps create a TypeScript-based MCP server in the specified directory, ready to be deployed into production.
Example server code
The generated MCP server includes a comprehensive file structure with TypeScript source code:
core.ts
build.mts
cli.ts
console-logger.ts
extensions.ts
mcp-server.ts
prompts.ts
resources.ts
scopes.ts
server.ts
shared.ts
tools.ts
Running your MCP server locally
You can run your MCP server from the generated code, a published npm package, or the generated MCP Bundle (MCPB) package.
Local development setup
For local development and debugging, you can run your MCP server directly from the generated code:
This configuration allows you to do the following:
Debug your server code directly.
Make real-time changes during development.
Test API integrations locally.
Validate tool functionality before distribution.
Testing with a published package
Once you’ve published your MCP server to npm (following our SDK publishing guide), you can test using the npm package format that will be used in production:
MCP servers with many tools can bloat LLM context windows, leading to increased token usage and tool confusion. Dynamic mode solves this by exposing only a small set of meta-tools that let agents progressively discover and invoke tools on demand.
To enable dynamic mode, pass the --mode dynamic flag when starting your server:
In dynamic mode, the server registers only the following meta-tools instead of every individual tool:
list_tools: Lists all available tools with their names and descriptions.
describe_tool: Returns the input schema for one or more tools by name.
execute_tool: Executes a tool by name with the provided input parameters.
list_scopes: Lists the scopes available on the server. Only registered if the server has scopes defined.
This approach significantly reduces the number of tokens sent to the LLM on each request, which is especially useful for servers with a large number of tools.
You can combine dynamic mode with scope and tool filters:
Speakeasy generates MCP servers with three primary distribution methods. You can distribute your server as an npm package, a Cloudflare Worker, or an MCPB file. These methods aren’t mutually exclusive — most customers use all three approaches to serve different use cases.
Distribute your MCP server on npm
Publishing your MCP server as an npm package is the most common starting point, as it provides the most flexible distribution method, in addition to the following benefits:
Universal compatibility: You can use a single command that works with all major MCP clients.
Easy customization: Users can modify tools, add tools, and extend functionality.
Version management: Standard npm versioning and dependency management apply.
Team sharing: Distribution across development teams is simple.
In addition to a drag-and-drop experience for end users (particularly in Claude Desktop), MCPB provides the following benefits:
User-friendly installation: Drag-and-drop installation in Claude Desktop.
Guided setup: Nicely prompts end-users for API keys and auth credentials.
Self-contained format: All dependencies and metadata bundled together.
Cross-platform compatibility: Works across different operating systems and MCP clients.
Speakeasy automatically generates MCP Bundle (.mcpb files) as part of MCP server generation. The MCPB includes:
A manifest.json file, containing metadata describing your MCP server
All the necessary server files, packaged for distribution
Tool descriptions and parameters, automatically inferred from your OpenAPI document
Icon and branding assets, if provided
You can customize the generated MCPB manifest through the gen.yaml configuration file:
targets: mcp-typescript: mcpbManifestOverlay: icon: "https://example.com/my-icon.png" displayName: "My Custom API Tools" description: "Custom description for my MCP server" version: "1.0.0"
Hybrid distribution strategy
You can use all three distribution methods simultaneously, giving users the flexibility to choose their preferred installation method based on their specific needs and technical requirements.
Response validation
By default, Speakeasy-generated MCP servers skip structured deserialization and Zod validation on HTTP responses. Instead, the raw response data is passed through directly as MCP tool results. This default behavior provides forward compatibility, allowing MCP tool calls to return data even if the API response doesn’t perfectly match the OpenAPI JSON Schema.
You can control this behavior using the validateResponse flag in gen.yaml:
mcp-typescript: validateResponse: false
When validateResponse is set to false (the default for new servers), no Zod schema validation is performed on HTTP response data. The raw data is passed through as MCP tool results, with minimal processing such as base64 encoding for binary image and audio data.
When validateResponse is set to true, HTTP responses are validated using generated Zod schemas before being returned as tool results. This provides strict type checking but may cause tool calls to fail if the API response contains fields or values that don’t conform to the OpenAPI specification.
Disabling validation is useful when:
The API may return additional fields not described in the OpenAPI specification.
There is little value in blocking LLM agents from reading data that may have minor schema inconsistencies.
You want to maximize the reliability of your MCP server by allowing it to handle API evolution gracefully.