Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

mcp-codegen.md

126 lines4.5 KB

1---
2title: MCP Type Generation
3id: mcp-codegen
4order: 11
5description: "Generate per-server TypeScript interface types from a live MCP server and wire them into createMCPClient for typed tool names and compile-checked pool keys."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - codegen
11 - type safety
12 - mcp.config.ts
13 - defineConfig
14 - createMCPClient
15 - generated types
16---
17​
18You have a running MCP server and you call its tools through [`createMCPClient`](./mcp), but nothing checks the tool names you reference at compile time. By the end of this guide you'll have generated per-server `interface` types from the live server and wired them into `createMCPClient`, narrowing discovered tool names to the server's literal names and compile-checking pool config keys — with zero runtime overhead. This is [Mode 3](./mcp#mode-3--generated-types-createmcpclientgeneratedserver) of MCP type safety. (Tool *arguments* stay untyped on the discovery path — for Zod-validated, TypeScript-typed arguments, combine with the [`tools([toolDefinition(...)])` overload](./mcp#mode-2--explicit-definitions-clienttoolsdefs).)
19​
20The `generate` CLI introspects a live MCP server and emits TypeScript interface types that you pass as a generic to `createMCPClient` / `createMCPClients`.
21​
22## 1. Create `mcp.config.ts`
23​
24Declare each server you want to generate types for. The `defineConfig` helper gives the config file full type checking and autocomplete.
25​
26```ts
27// mcp.config.ts
28import { defineConfig } from '@tanstack/ai-mcp'
29​
30export default defineConfig({
31 servers: {
32 github: {
33 transport: { type: 'http', url: 'https://github-mcp.example.com/mcp' },
34 },
35 linear: {
36 transport: { type: 'http', url: 'https://linear-mcp.example.com/mcp' },
37 prefix: 'linear', // must match runtime createMCPClient({ prefix })
38 },
39 },
40 outFile: './mcp-types.generated.ts',
41})
42```
43​
44> `createMCPClient` tries protocol `2026-07-28` first. If the server does not support that protocol, the client uses the 2025 initialize handshake. The generator uses this handshake.
46> Package names for an `@modelcontextprotocol/sdk` import are in [MCP SDK packages](../migration/mcp-sdk).
47​
48## 2. Run the generator
49​
50```bash
51npx @tanstack/ai-mcp generate
52```
53​
54The CLI connects to each declared server, introspects its tools, resources, and prompts, and writes the result to `outFile`.
55​
56## 3. Inspect the output
57​
58The generator emits one interface per server plus a combined pool map:
59​
60```ts
61// AUTO-GENERATED by `npx @tanstack/ai-mcp generate`. Do not edit.
62​
63import type { ServerDescriptor } from '@tanstack/ai-mcp'
64​
65export interface GithubServer extends ServerDescriptor {
66 tools: {
67 'search_repositories': { input: { query: string; limit?: number }; output: unknown }
68 'create_issue': { input: { repo: string; title: string; body?: string }; output: unknown }
69 }
70 resources: {}
71 prompts: {}
72 capabilities: { tools: {} } & Record<string, unknown>
74​
75export interface LinearServer extends ServerDescriptor {
76 tools: {
77 'linear_create_issue': { input: { title: string; teamId: string }; output: unknown }
78 }
79 resources: {}
80 prompts: {}
81 capabilities: { tools: {} } & Record<string, unknown>
83​
84export interface MCPServers extends Record<string, ServerDescriptor> {
85 'github': GithubServer
86 'linear': LinearServer
88```
89​
90## 4. Use generated types at runtime
91​
92Pass the generated type as a generic to [`createMCPClient`](./mcp) (single server) or `createMCPClients` (pool). Tool names are narrowed to the literal types declared by the server, so a typo is a compile error.
93​
94**Single server:**
95​
96```ts
97import type { GithubServer } from './mcp-types.generated'
98import { createMCPClient } from '@tanstack/ai-mcp'
99​
100const mcp = await createMCPClient<GithubServer>({
101 transport: { type: 'http', url: process.env.GITHUB_MCP_URL! },
102})
103​
104const tools = await mcp.tools()
105// Each tool name is narrowed from GithubServer['tools']
106```
107​
108**Multi-server pool:**
109​
110```ts
111import type { MCPServers } from './mcp-types.generated'
112import { createMCPClients } from '@tanstack/ai-mcp'
113​
114const pool = await createMCPClients<MCPServers>({
115 github: { transport: { type: 'http', url: process.env.GITHUB_MCP_URL! } },
116 linear: {
117 transport: { type: 'http', url: process.env.LINEAR_MCP_URL! },
118 prefix: 'linear',
119 },
120})
121​
122// Config keys are constrained to the declared servers — a typo is a compile error
123const tools = await pool.tools()
124```
125​
126Now that tool names and pool keys are compile-checked, hand the generated client to `chat()`. See [Managed MCP with `chat()`](./mcp-managed) to let `chat()` own discovery and lifecycle.