|
1 | 1 | import { renderHook, act } from "@testing-library/react"; |
2 | 2 | import { useConnection } from "../useConnection"; |
3 | 3 | import { z } from "zod"; |
4 | | -import { ClientRequest } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | +import { |
| 5 | + ClientRequest, |
| 6 | + JSONRPCMessage, |
| 7 | +} from "@modelcontextprotocol/sdk/types.js"; |
5 | 8 | import { DEFAULT_INSPECTOR_CONFIG, CLIENT_IDENTITY } from "../../constants"; |
6 | 9 | import { |
7 | 10 | SSEClientTransportOptions, |
@@ -42,10 +45,12 @@ const mockSSETransport: { |
42 | 45 | start: jest.Mock; |
43 | 46 | url: URL | undefined; |
44 | 47 | options: SSEClientTransportOptions | undefined; |
| 48 | + onmessage?: (message: JSONRPCMessage) => void; |
45 | 49 | } = { |
46 | 50 | start: jest.fn(), |
47 | 51 | url: undefined, |
48 | 52 | options: undefined, |
| 53 | + onmessage: undefined, |
49 | 54 | }; |
50 | 55 |
|
51 | 56 | const mockStreamableHTTPTransport: { |
@@ -480,6 +485,57 @@ describe("useConnection", () => { |
480 | 485 | }); |
481 | 486 | }); |
482 | 487 |
|
| 488 | + describe("Ref Resolution", () => { |
| 489 | + beforeEach(() => { |
| 490 | + jest.clearAllMocks(); |
| 491 | + }); |
| 492 | + |
| 493 | + test("resolves $ref references in requestedSchema properties before validation", async () => { |
| 494 | + const mockProtocolOnMessage = jest.fn(); |
| 495 | + |
| 496 | + mockSSETransport.onmessage = mockProtocolOnMessage; |
| 497 | + |
| 498 | + const { result } = renderHook(() => useConnection(defaultProps)); |
| 499 | + |
| 500 | + await act(async () => { |
| 501 | + await result.current.connect(); |
| 502 | + }); |
| 503 | + |
| 504 | + const mockRequestWithRef: JSONRPCMessage = { |
| 505 | + jsonrpc: "2.0", |
| 506 | + id: 1, |
| 507 | + method: "elicitation/create", |
| 508 | + params: { |
| 509 | + message: "Please provide your information", |
| 510 | + requestedSchema: { |
| 511 | + type: "object", |
| 512 | + properties: { |
| 513 | + name: { |
| 514 | + $ref: "#/properties/nameDef", |
| 515 | + }, |
| 516 | + nameDef: { |
| 517 | + type: "string", |
| 518 | + title: "Name", |
| 519 | + }, |
| 520 | + }, |
| 521 | + }, |
| 522 | + }, |
| 523 | + }; |
| 524 | + |
| 525 | + await act(async () => { |
| 526 | + mockSSETransport.onmessage!(mockRequestWithRef); |
| 527 | + }); |
| 528 | + |
| 529 | + expect(mockProtocolOnMessage).toHaveBeenCalledTimes(1); |
| 530 | + |
| 531 | + const message = mockProtocolOnMessage.mock.calls[0][0]; |
| 532 | + expect(message.params.requestedSchema.properties.name).toEqual({ |
| 533 | + type: "string", |
| 534 | + title: "Name", |
| 535 | + }); |
| 536 | + }); |
| 537 | + }); |
| 538 | + |
483 | 539 | describe("URL Port Handling", () => { |
484 | 540 | const SSEClientTransport = jest.requireMock( |
485 | 541 | "@modelcontextprotocol/sdk/client/sse.js", |
|
0 commit comments