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

mcp-codegen.md

130 lines3.7 KB

1---
2title: MCP Type Generation
3id: mcp-codegen
4order: 11
5description: "Generate TypeScript types from a live MCP server, and pass them to createMCPClient to check tool names and pool keys at compile time."
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​
18Generate TypeScript types from a live MCP server and pass them to `createMCPClient`. A wrong tool name is then a type error.
19​
20You get:
21​
22- Tool names narrowed to the literal names of the server.
23- Compile-checked config keys on a pool.
24- No runtime cost.
25​
26Tool arguments stay `unknown` on this path. For typed arguments, also use the [`tools([toolDefinition(...)])` form](./mcp-advanced#mode-2-explicit-definitions).
27​
28## 1. Create `mcp.config.ts`
29​
30List each server that you want types for. `defineConfig` gives the file type checks and autocomplete.
31​
32```ts
33// mcp.config.ts
34import { defineConfig } from '@tanstack/ai-mcp'
35​
36export default defineConfig({
37 servers: {
38 github: {
39 transport: { type: 'http', url: 'https://github-mcp.example.com/mcp' },
40 },
41 linear: {
42 transport: { type: 'http', url: 'https://linear-mcp.example.com/mcp' },
43 prefix: 'linear', // must match runtime createMCPClient({ prefix })
44 },
45 },
46 outFile: './mcp-types.generated.ts',
47})
48```
49​
50## 2. Run the generator
51​
52```bash
53npx @tanstack/ai-mcp generate
54```
55​
56The CLI connects to each server, reads its tools, resources, and prompts, and writes the types to `outFile`. It uses the same handshake as `createMCPClient`: protocol `2026-07-28` first, then the 2025 handshake.
57​
58## 3. Read the output
59​
60The generator writes one interface for each server, plus a map for the pool:
61​
62```ts
63// AUTO-GENERATED by `npx @tanstack/ai-mcp generate`. Do not edit.
64​
65import type { ServerDescriptor } from '@tanstack/ai-mcp'
66​
67export interface GithubServer extends ServerDescriptor {
68 tools: {
69 'search_repositories': { input: { query: string; limit?: number }; output: unknown }
70 'create_issue': { input: { repo: string; title: string; body?: string }; output: unknown }
71 }
72 resources: {}
73 prompts: {}
74 capabilities: { tools: {} } & Record<string, unknown>
76​
77export interface LinearServer extends ServerDescriptor {
78 tools: {
79 'linear_create_issue': { input: { title: string; teamId: string }; output: unknown }
80 }
81 resources: {}
82 prompts: {}
83 capabilities: { tools: {} } & Record<string, unknown>
85​
86export interface MCPServers extends Record<string, ServerDescriptor> {
87 'github': GithubServer
88 'linear': LinearServer
90```
91​
92## 4. Use the types
93​
94Pass the type as a generic to `createMCPClient` (one server) or `createMCPClients` (a pool).
95​
96One server:
97​
98```ts
99import type { GithubServer } from './mcp-types.generated'
100import { createMCPClient } from '@tanstack/ai-mcp'
101​
102const mcp = await createMCPClient<GithubServer>({
103 transport: { type: 'http', url: process.env.GITHUB_MCP_URL! },
104})
105​
106const tools = await mcp.tools()
107// Each tool name is narrowed from GithubServer['tools']
108```
109​
110A pool:
111​
112```ts
113import type { MCPServers } from './mcp-types.generated'
114import { createMCPClients } from '@tanstack/ai-mcp'
115​
116const pool = await createMCPClients<MCPServers>({
117 github: { transport: { type: 'http', url: process.env.GITHUB_MCP_URL! } },
118 linear: {
119 transport: { type: 'http', url: process.env.LINEAR_MCP_URL! },
120 prefix: 'linear',
121 },
122})
123​
124// The config keys must be declared servers. A typo is a compile error.
125const tools = await pool.tools()
126```
127​
128Now a misspelled tool name or pool key fails the type check. To give the typed client to `chat()`, see [Managed MCP with chat()](./mcp-managed).
129​
130Package names for an `@modelcontextprotocol/sdk` import are in [MCP SDK packages](../migration/mcp-sdk).