|
1 | | -import type { JSONRPCMessage } from '@modelcontextprotocol/core'; |
| 1 | +import type { CallToolResultWithStructuredContent, JSONRPCMessage } from '@modelcontextprotocol/core'; |
2 | 2 | import { InMemoryTransport, isStandardSchema, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core'; |
3 | 3 | import { describe, expect, expectTypeOf, it, vi } from 'vitest'; |
4 | 4 | import * as z from 'zod/v4'; |
@@ -119,6 +119,50 @@ describe('registerTool/registerPrompt accept raw Zod shape (auto-wrapped)', () = |
119 | 119 |
|
120 | 120 | await server.close(); |
121 | 121 | }); |
| 122 | + |
| 123 | + it('adds text fallback for structuredContent when an untyped handler omits content', async () => { |
| 124 | + const server = new McpServer({ name: 't', version: '1.0.0' }); |
| 125 | + const untypedHandler = (async () => ({ structuredContent: [1, 2, 3] })) as unknown as () => Promise< |
| 126 | + CallToolResultWithStructuredContent<number[]> |
| 127 | + >; |
| 128 | + |
| 129 | + server.registerTool('forecast', { outputSchema: z.array(z.number()) }, untypedHandler); |
| 130 | + |
| 131 | + const [client, srv] = InMemoryTransport.createLinkedPair(); |
| 132 | + await server.connect(srv); |
| 133 | + await client.start(); |
| 134 | + |
| 135 | + const responses: JSONRPCMessage[] = []; |
| 136 | + client.onmessage = m => responses.push(m); |
| 137 | + |
| 138 | + await client.send({ |
| 139 | + jsonrpc: '2.0', |
| 140 | + id: 1, |
| 141 | + method: 'initialize', |
| 142 | + params: { |
| 143 | + protocolVersion: LATEST_PROTOCOL_VERSION, |
| 144 | + capabilities: {}, |
| 145 | + clientInfo: { name: 'c', version: '1.0.0' } |
| 146 | + } |
| 147 | + } as JSONRPCMessage); |
| 148 | + await client.send({ jsonrpc: '2.0', method: 'notifications/initialized' } as JSONRPCMessage); |
| 149 | + await client.send({ |
| 150 | + jsonrpc: '2.0', |
| 151 | + id: 2, |
| 152 | + method: 'tools/call', |
| 153 | + params: { name: 'forecast', arguments: {} } |
| 154 | + } as JSONRPCMessage); |
| 155 | + |
| 156 | + await vi.waitFor(() => expect(responses.some(r => 'id' in r && r.id === 2)).toBe(true)); |
| 157 | + |
| 158 | + const response = responses.find(r => 'id' in r && r.id === 2) as { |
| 159 | + result?: { content?: Array<{ type: string; text?: string }>; structuredContent?: unknown }; |
| 160 | + }; |
| 161 | + expect(response.result?.structuredContent).toEqual([1, 2, 3]); |
| 162 | + expect(response.result?.content).toEqual([{ type: 'text', text: '[1,2,3]' }]); |
| 163 | + |
| 164 | + await server.close(); |
| 165 | + }); |
122 | 166 | }); |
123 | 167 |
|
124 | 168 | describe('InferRawShape', () => { |
|
0 commit comments