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

api.mcp-status-test.ts

87 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 { toolCallId: 'task-e2e', emitCustomEvent: () => {} },
54 )
55​
56 const resourceList = await client.resources().catch(() => [])
57 const resourceContent: Array<unknown> = []
58 for (const r of resourceList) {
59 const read = await client.readResource(r.uri)
60 for (const c of read.contents) {
61 resourceContent.push(mcpResourceToContentPart(c))
62 }
63 }
64​
65 const promptList = await client.prompts().catch(() => [])
66 const promptMessages: Array<unknown> = []
67 for (const p of promptList) {
68 const got = await client.getPrompt(p.name)
69 promptMessages.push(...mcpPromptToMessages(got))
70 }
71​
72 return Response.json({
73 tools,
74 taskResult,
75 toolMeta,
76 resources: resourceList.map((r) => r.uri),
77 prompts: promptList.map((p) => p.name),
78 resourceContent,
79 promptMessages,
80 })
81 } finally {
82 await client.close()
83 }
84 },
85 },
86 },
87})