- Model Context Protocol (MCP): An emerging standard for AI models to interact with tools and APIs in real time. Building an MCP server involves exposing tools that an LLM can call. We aim to generate an MCP server from the existing OpenAPI specification to ensure quick development and consistency.
- Benefits and Challenges of Auto-generating MCP Tools: Automatically generating tools from a REST API spec saves time and keeps API and MCP definitions in sync. However, a naive one-to-one mapping can overwhelm an LLM. A balanced approach is needed by autogenerating the groundwork from OpenAPI and then curating it.
Building the Xata MCP Server:
- Migrating to Kubb for OpenAPI code generation: We replaced the traditional OpenAPI code generator with Kubb, a toolkit for generating code from OpenAPI/Swagger specs. Kubb's plugin and generator architecture allows for flexible customization and the generation of multiple outputs.
- Customizing Kubb with custom generators: We wrote custom generators using Kubb's extensibility to produce a TypeScript API client and MCP tool handlers. The API client generator creates a lightweight API client library that stays up-to-date with the API. The MCP tool generator customizes the MCP interface and uses Zod for input validation.
- Creating the MCP Server with Next.js: We chose Next.js to implement the MCP server using Vercel's
@vercel/mcp-adapter
. The server handles route handling with dynamic routes to support different transports. It also includes authentication middleware and uses Redis for Server-Sent Events (SSE) state management. The MCP server offers a conversational interface to the platform, turning natural language prompts into API actions.
- Conclusion: Leveraging the OpenAPI schema gives the MCP server the ability to evolve with the API and ensures strong typing and validation. This hybrid approach saves time and enables smart app development. Xata's latest offering, "Postgres at scale" with data branching and PII anonymization, is live and invites experimentation.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。