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

mcp-status.spec.ts

147 lines5.1 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * MCP resource + prompt read/convert path, end-to-end against the in-process
5 * Streamable-HTTP MCP server (`api.mcp-server`).
6 *
7 * `api.mcp-status-test` connects via `@tanstack/ai-mcp`, lists + reads the
8 * server's tools/resources/prompts, and converts resources/prompts through the
9 * public `mcpResourceToContentPart` / `mcpPromptToMessages` helpers. This proves
10 * the manual-integration building blocks (resources(), readResource(),
11 * prompts(), getPrompt() + converters) work against a real server — the unit
12 * tests only cover the converters in isolation. No LLM is involved.
13 */
14test.describe('mcp — resource/prompt discovery + conversion', () => {
15 test('lists and converts the server tools, resources, and prompts', async ({
16 request,
17 }) => {
18 const res = await request.get('/api/mcp-status-test')
19 const body = await res.text()
20 expect(
21 res.ok(),
22 `mcp-status-test route failed (${res.status()}): ${body}`,
23 ).toBe(true)
24​
25 const json = JSON.parse(body) as {
26 tools: Array<string>
27 taskResult: unknown
28 toolMeta: Array<{
29 name: string
30 title?: string
31 annotations?: Record<string, unknown>
32 }>
33 resources: Array<string>
34 prompts: Array<string>
35 resourceContent: Array<{ type: string; content: string }>
36 promptMessages: Array<{ role: string; content: string }>
37 }
38​
39 // Tool discovered.
40 expect(json.tools).toContain('get_guitar_price')
41​
42 // The server's display title + behavior annotations are forwarded onto the
43 // discovered tool's `metadata.mcp` — a host can label the tool and shape
44 // its approval UI without a second tools/list round-trip.
45 const priceMeta = json.toolMeta.find((t) => t.name === 'get_guitar_price')
46 expect(priceMeta?.title).toBe('Guitar Price Lookup')
47 expect(priceMeta?.annotations).toEqual({
48 readOnlyHint: true,
49 destructiveHint: false,
50 idempotentHint: true,
51 openWorldHint: false,
52 })
53​
54 // Task-required tools are discovered and execute through MCP's task stream.
55 expect(json.tools).toContain('appraise_guitar_collection')
56 expect(JSON.stringify(json.taskResult)).toContain('4200')
57 expect(JSON.stringify(json.taskResult)).toContain('strat')
58​
59 // Resource listed + read + converted to a text ContentPart carrying the
60 // server's distinctive token.
61 expect(json.resources).toContain('guitar://catalog')
62 const resourceText = json.resourceContent.map((c) => c.content).join('\n')
63 expect(resourceText).toContain('STRAT-001')
64​
65 // Prompt listed + fetched + converted to ModelMessages.
66 expect(json.prompts).toContain('recommend_guitar')
67 expect(json.promptMessages.length).toBeGreaterThan(0)
68 const promptText = json.promptMessages.map((m) => m.content).join('\n')
69 expect(promptText.toLowerCase()).toContain('guitar')
70 })
71})
72​
73test.describe('mcp — task execution error paths', () => {
74 test('aborts a hanging task-required tool without waiting for a result', async ({
75 request,
76 }) => {
77 const res = await request.get('/api/mcp-task-errors?scenario=abort')
78 const body = await res.text()
79 expect(
80 res.ok(),
81 `mcp-task-errors abort failed (${res.status()}): ${body}`,
82 ).toBe(true)
83 const json = JSON.parse(body) as {
84 aborted: boolean
85 elapsedMs: number
86 errorName: string
87 message: string
88 }
89 expect(json.aborted).toBe(true)
90 expect(json.elapsedMs).toBeLessThan(5000)
91 expect(json.message).not.toContain('4200')
92 expect(json.errorName).not.toBe('none')
93 })
94​
95 test('skips a task-required tool when the server has no tasks capability', async ({
96 request,
97 }) => {
98 const res = await request.get(
99 '/api/mcp-task-errors?scenario=skip-unsupported',
100 )
101 const body = await res.text()
102 expect(
103 res.ok(),
104 `mcp-task-errors skip-unsupported failed (${res.status()}): ${body}`,
105 ).toBe(true)
106 const json = JSON.parse(body) as { tools: Array<string> }
107 expect(json.tools).toEqual(['plain_tool'])
108 })
109​
110 test('callTool throws MCPTaskRequiredToolError without the tasks capability', async ({
111 request,
112 }) => {
113 const res = await request.get(
114 '/api/mcp-task-errors?scenario=call-unsupported',
115 )
116 const body = await res.text()
117 expect(
118 res.ok(),
119 `mcp-task-errors call-unsupported failed (${res.status()}): ${body}`,
120 ).toBe(true)
121 const json = JSON.parse(body) as {
122 errorName: string
123 isTaskRequired: boolean
124 }
125 expect(json.errorName).toBe('MCPTaskRequiredToolError')
126 expect(json.isTaskRequired).toBe(true)
127 })
128​
129 test('tools([defs]) throws MCPTaskRequiredToolError without the tasks capability', async ({
130 request,
131 }) => {
132 const res = await request.get(
133 '/api/mcp-task-errors?scenario=bind-unsupported',
134 )
135 const body = await res.text()
136 expect(
137 res.ok(),
138 `mcp-task-errors bind-unsupported failed (${res.status()}): ${body}`,
139 ).toBe(true)
140 const json = JSON.parse(body) as {
141 errorName: string
142 isTaskRequired: boolean
143 }
144 expect(json.errorName).toBe('MCPTaskRequiredToolError')
145 expect(json.isTaskRequired).toBe(true)
146 })
147})