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

api.mcp-status-test.ts

91 lines3.2 KB

1import { createFileRoute } from '@tanstack/react-router'
2import {
3 createMCPClient,
4 mcpPromptToMessages,
5 mcpResourceToContentPart,
6} from '@tanstack/ai-mcp'
7​
8/**
9 * Capability-probe endpoint for the in-process MCP server (`api.mcp-server`).
10 *
11 * Connects via `@tanstack/ai-mcp`, then lists + reads the server's tools,
12 * resources, and prompts — converting resources/prompts through the public
13 * `mcpResourceToContentPart` / `mcpPromptToMessages` helpers. Returns the
14 * result as JSON so a spec can validate the resource/prompt read+convert path
15 * end-to-end against a real Streamable-HTTP MCP server, with no LLM involved.
16 *
17 * `toolMeta` additionally carries each discovered tool's forwarded MCP
18 * metadata (display `title` + behavior `annotations`), proving the server's
19 * hints survive discovery and reach the host.
20 *
21 * No aimock dependency: this exercises only the MCP client surface.
22 */
23export const Route = createFileRoute('/api/mcp-status-test')({
24 server: {
25 handlers: {
26 GET: async ({ request }) => {
27 const origin = new URL(request.url).origin
28 const mcpUrl = `${origin}/api/mcp-server`
29​
30 const client = await createMCPClient({
31 transport: { type: 'http', url: mcpUrl },
32 })
33 try {
34 const discovered = await client.tools()
35 const tools = discovered.map((t) => t.name)
36 // The server's display title + behavior annotations, as forwarded
37 // onto `metadata.mcp` by discovery. `tools()` returns
38 // `McpServerTool`s, so this reads straight through — typed, no
39 // annotation, no cast.
40 const toolMeta = discovered.map((t) => ({
41 name: t.name,
42 title: t.metadata.mcp.title,
43 annotations: t.metadata.mcp.annotations,
44 }))
45 const taskTool = discovered.find(
46 (tool) => tool.name === 'appraise_guitar_collection',
47 )
48 if (!taskTool?.execute) {
49 throw new Error('Task-required appraisal tool was not discovered')
50 }
51 const taskResult = await taskTool.execute(
52 { ids: ['strat', 'tele'] },
53 {
54 toolCallId: 'task-e2e',
55 threadId: 'mcp-status-test',
56 emitCustomEvent: () => {},
57 },
58 )
59​
60 const resourceList = await client.resources().catch(() => [])
61 const resourceContent: Array<unknown> = []
62 for (const r of resourceList) {
63 const read = await client.readResource(r.uri)
64 for (const c of read.contents) {
65 resourceContent.push(mcpResourceToContentPart(c))
66 }
67 }
68​
69 const promptList = await client.prompts().catch(() => [])
70 const promptMessages: Array<unknown> = []
71 for (const p of promptList) {
72 const got = await client.getPrompt(p.name)
73 promptMessages.push(...mcpPromptToMessages(got))
74 }
75​
76 return Response.json({
77 tools,
78 taskResult,
79 toolMeta,
80 resources: resourceList.map((r) => r.uri),
81 prompts: promptList.map((p) => p.name),
82 resourceContent,
83 promptMessages,
84 })
85 } finally {
86 await client.close()
87 }
88 },
89 },
90 },
91})