|
7 | 7 | ListResourceTemplatesResult, |
8 | 8 | ListResourcesResult, |
9 | 9 | ReadResourceResult, |
| 10 | + ListPromptsResult, |
| 11 | + GetPromptResult, |
10 | 12 | } from './types'; |
11 | 13 | import { |
12 | 14 | beforeEach, |
@@ -148,6 +150,80 @@ describe('MCPClient', () => { |
148 | 150 | `); |
149 | 151 | }); |
150 | 152 |
|
| 153 | + it('should list prompts from the server', async () => { |
| 154 | + client = await createMCPClient({ |
| 155 | + transport: { type: 'sse', url: 'https://example.com/sse' }, |
| 156 | + }); |
| 157 | + |
| 158 | + const prompts = await client.listPrompts(); |
| 159 | + |
| 160 | + expectTypeOf(prompts).toEqualTypeOf<ListPromptsResult>(); |
| 161 | + |
| 162 | + expect(prompts.prompts).toMatchInlineSnapshot(` |
| 163 | + [ |
| 164 | + { |
| 165 | + "arguments": [ |
| 166 | + { |
| 167 | + "description": "The code to review", |
| 168 | + "name": "code", |
| 169 | + "required": true, |
| 170 | + }, |
| 171 | + ], |
| 172 | + "description": "Asks the LLM to analyze code quality and suggest improvements", |
| 173 | + "name": "code_review", |
| 174 | + "title": "Request Code Review", |
| 175 | + }, |
| 176 | + ] |
| 177 | + `); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should get a prompt by name', async () => { |
| 181 | + client = await createMCPClient({ |
| 182 | + transport: { type: 'sse', url: 'https://example.com/sse' }, |
| 183 | + }); |
| 184 | + |
| 185 | + const prompt = await client.getPrompt({ |
| 186 | + name: 'code_review', |
| 187 | + arguments: { code: 'print(42)' }, |
| 188 | + }); |
| 189 | + |
| 190 | + expectTypeOf(prompt).toEqualTypeOf<GetPromptResult>(); |
| 191 | + |
| 192 | + expect(prompt).toMatchInlineSnapshot(` |
| 193 | + { |
| 194 | + "description": "Code review prompt", |
| 195 | + "messages": [ |
| 196 | + { |
| 197 | + "content": { |
| 198 | + "text": "Please review this code:\nfunction add(a, b) { return a + b; }", |
| 199 | + "type": "text", |
| 200 | + }, |
| 201 | + "role": "user", |
| 202 | + }, |
| 203 | + ], |
| 204 | + } |
| 205 | + `); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should throw if the server does not support prompts', async () => { |
| 209 | + createMockTransport.mockImplementation( |
| 210 | + () => |
| 211 | + new MockMCPTransport({ |
| 212 | + resources: [], |
| 213 | + prompts: [], |
| 214 | + }), |
| 215 | + ); |
| 216 | + |
| 217 | + client = await createMCPClient({ |
| 218 | + transport: { type: 'sse', url: 'https://example.com/sse' }, |
| 219 | + }); |
| 220 | + |
| 221 | + await expect(client.listPrompts()).rejects.toThrow(MCPClientError); |
| 222 | + await expect(client.getPrompt({ name: 'code_review' })).rejects.toThrow( |
| 223 | + MCPClientError, |
| 224 | + ); |
| 225 | + }); |
| 226 | + |
151 | 227 | it('should return typed AI SDK compatible tool set when schemas are provided', async () => { |
152 | 228 | const mockTransport = new MockMCPTransport({ |
153 | 229 | overrideTools: [ |
|
0 commit comments