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

cli-introspect.test.ts

136 lines3.8 KB

1import { InMemoryTransport } from '@modelcontextprotocol/client'
2import { Server } from '@modelcontextprotocol/server'
3import { describe, expect, it } from 'vitest'
4import { introspectFromTransport } from '../src/cli/introspect'
5import {
6 makeFullServer,
7 makeServerWithLoopingCursor,
8 makeServerWithPaginatedTools,
9 makeServerWithWeatherTool,
10} from './helpers/in-memory-server'
11​
12async function readOpened(
13 opened: Promise<{
14 server: { close: () => Promise<void> }
15 clientTransport: Parameters<typeof introspectFromTransport>[0]
16 }>,
17) {
18 const { server, clientTransport } = await opened
19 try {
20 return await introspectFromTransport(clientTransport)
21 } finally {
22 await server.close()
23 }
25​
26describe('introspect', () => {
27 it('reads the full server surface', async () => {
28 const surface = await readOpened(makeFullServer())
29​
30 expect(surface.tools).toEqual([
31 {
32 name: 'get_weather',
33 description: 'Get weather for a city',
34 inputSchema: {
35 $schema: 'https://json-schema.org/draft/2020-12/schema',
36 type: 'object',
37 properties: {
38 city: { type: 'string' },
39 },
40 required: ['city'],
41 },
42 outputSchema: undefined,
43 },
44 ])
45 expect(surface.resources).toEqual([
46 { uri: 'file:///hello.txt', name: 'hello' },
47 ])
48 expect(surface.prompts).toEqual([
49 {
50 name: 'review-code',
51 arguments: [{ name: 'code', required: true }],
52 },
53 ])
54 expect(surface.capabilities).toEqual({
55 tools: { listChanged: true },
56 resources: { listChanged: true },
57 prompts: { listChanged: true },
58 })
59 })
60​
61 it('returns empty resources and prompts when the server does not list them', async () => {
62 const surface = await readOpened(makeServerWithWeatherTool())
63​
64 expect(surface.tools.map((tool) => tool.name)).toEqual(['get_weather'])
65 expect(surface.resources).toEqual([])
66 expect(surface.prompts).toEqual([])
67 expect(surface.capabilities).toEqual({
68 tools: { listChanged: true },
69 })
70 })
71​
72 it('reads tools from every list page', async () => {
73 const surface = await readOpened(makeServerWithPaginatedTools())
74​
75 expect(surface.tools).toEqual([
76 {
77 name: 'first_page_tool',
78 description: 'On page one',
79 inputSchema: { type: 'object' },
80 outputSchema: undefined,
81 },
82 {
83 name: 'second_page_tool',
84 description: 'On page two',
85 inputSchema: { type: 'object' },
86 outputSchema: undefined,
87 },
88 ])
89 expect(surface.resources).toEqual([])
90 expect(surface.prompts).toEqual([])
91 })
92​
93 it('throws when a list repeats a cursor', async () => {
94 await expect(readOpened(makeServerWithLoopingCursor())).rejects.toThrow(
95 'MCP list pagination repeated a cursor',
96 )
97 })
98​
99 it('reads tools from a server that only speaks the 2025 handshake', async () => {
100 const server = new Server(
101 { name: 'legacy', version: '1.0.0' },
102 {
103 capabilities: { tools: {} },
104 supportedProtocolVersions: ['2025-11-25'],
105 },
106 )
107 server.setRequestHandler('tools/list', () => ({
108 tools: [
109 {
110 name: 'legacy_tool',
111 description: 'Only on the 2025 handshake',
112 inputSchema: { type: 'object' },
113 },
114 ],
115 }))
116 const [clientTransport, serverTransport] =
117 InMemoryTransport.createLinkedPair()
118 await server.connect(serverTransport)
119​
120 const surface = await readOpened(
121 Promise.resolve({ server, clientTransport }),
122 )
123​
124 expect(surface.tools).toEqual([
125 {
126 name: 'legacy_tool',
127 description: 'Only on the 2025 handshake',
128 inputSchema: { type: 'object' },
129 outputSchema: undefined,
130 },
131 ])
132 expect(surface.resources).toEqual([])
133 expect(surface.prompts).toEqual([])
134 expect(surface.capabilities).toEqual({ tools: {} })
135 })
136})