Open History to see the last commit
1import { Client } from '@modelcontextprotocol/client'
2import type { Transport } from '@modelcontextprotocol/client'
3import { listPages } from '../list-pages'
4import { resolveTransport } from '../transport'
5import type { CodegenServerConfig } from './define-config'
6
7/**
8 * Tools, resources, prompts, and capabilities read from one MCP server.
9 */
10export interface ServerSurface {
11 tools: Array<{
12 name: string
13 inputSchema: unknown
14 outputSchema?: unknown
15 description?: string
16 }>
17 resources: Array<{ uri: string; name?: string }>
18 prompts: Array<{
19 name: string
20 arguments?: Array<{ name: string; required?: boolean }>
21 }>
22 capabilities: Record<string, unknown>
23}
24
25// A list call with no cursor reads every page and removes nextCursor.
26// listPages must see each cursor. A repeated cursor then throws an Error.
27function fetchListPage<
28 MethodName extends 'tools/list' | 'resources/list' | 'prompts/list',
29>(client: Client, method: MethodName, cursor: string | undefined) {
30 if (cursor === undefined) {
31 return client.request({ method })
32 }
33 const params: Record<string, unknown> = { cursor }
34 return client.request({ method, params })
35}
36
37/**
38 * Read tools, resources, prompts, and capabilities from one MCP server.
39 *
40 * `transport` is a client transport that is not connected yet.
41 * The client tries protocol `2026-07-28` first.
42 * If the server does not support that protocol, the client uses the 2025 initialize handshake.
43 * This function throws an Error when connect fails, when a list repeats a cursor, or when a list passes the page cap.
44 */
45export async function introspectFromTransport(transport: Transport) {
46 const client = new Client(
47 { name: 'tanstack-ai-mcp-codegen', version: '0.0.1' },
48 { versionNegotiation: { mode: 'auto' } },
49 )
50 try {
51 await client.connect(transport)
52 const reported = client.getServerCapabilities()
53 const serverListsTools = reported?.tools !== undefined
54 const serverListsResources = reported?.resources !== undefined
55 const serverListsPrompts = reported?.prompts !== undefined
56 const tools = serverListsTools
57 ? await listPages(async (cursor) => {
58 const page = await fetchListPage(client, 'tools/list', cursor)
59 return { items: page.tools, nextCursor: page.nextCursor }
60 })
61 : []
62 const resources = serverListsResources
63 ? await listPages(async (cursor) => {
64 const page = await fetchListPage(client, 'resources/list', cursor)
65 return { items: page.resources, nextCursor: page.nextCursor }
66 })
67 : []
68 const prompts = serverListsPrompts
69 ? await listPages(async (cursor) => {
70 const page = await fetchListPage(client, 'prompts/list', cursor)
71 return { items: page.prompts, nextCursor: page.nextCursor }
72 })
73 : []
74 const capabilities: Record<string, unknown> = {}
75 if (reported !== undefined) {
76 const entries = Object.entries(reported)
77 for (const [key, entry] of entries) {
78 capabilities[key] = entry
79 }
80 }
81 return {
82 tools: tools.map((tool) => ({
83 name: tool.name,
84 inputSchema: tool.inputSchema,
85 outputSchema: tool.outputSchema,
86 description: tool.description,
87 })),
88 resources: resources.map((resource) => ({
89 uri: resource.uri,
90 name: resource.name,
91 })),
92 prompts: prompts.map((prompt) => ({
93 name: prompt.name,
94 arguments: prompt.arguments,
95 })),
96 capabilities,
97 }
98 } finally {
99 // A close failure must not hide the original error.
100 await client.close().catch(() => undefined)
101 }
102}
103
104/**
105 * Connect to the server in `config` and read its surface.
106 *
107 * `config` is one server from the codegen config.
108 * This function throws an Error for the same failures as `introspectFromTransport`.
109 */
110export async function introspectServer(config: CodegenServerConfig) {
111 const transport = await resolveTransport(config.transport)
112 return introspectFromTransport(transport)
113}