-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from sumitesh9/sumiteshn9/fix-error-codes
Updates SDK error codes to use JSON-RPC server error range
- Loading branch information
Showing
2 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Protocol } from "./protocol.js"; | ||
import { Transport } from "./transport.js"; | ||
import { | ||
McpError, | ||
ErrorCode, | ||
Request, | ||
Result, | ||
Notification, | ||
} from "../types.js"; | ||
import { ZodType, z } from "zod"; | ||
|
||
// Mock Transport class | ||
class MockTransport implements Transport { | ||
onclose?: () => void; | ||
onerror?: (error: Error) => void; | ||
onmessage?: (message: unknown) => void; | ||
|
||
async start(): Promise<void> {} | ||
async close(): Promise<void> { | ||
this.onclose?.(); | ||
} | ||
async send(_message: unknown): Promise<void> {} | ||
} | ||
|
||
describe("protocol tests", () => { | ||
let protocol: Protocol<Request, Notification, Result>; | ||
let transport: MockTransport; | ||
|
||
beforeEach(() => { | ||
transport = new MockTransport(); | ||
protocol = new (class extends Protocol<Request, Notification, Result> { | ||
protected assertCapabilityForMethod(): void {} | ||
protected assertNotificationCapability(): void {} | ||
protected assertRequestHandlerCapability(): void {} | ||
})(); | ||
}); | ||
|
||
test("should throw a timeout error if the request exceeds the timeout", async () => { | ||
await protocol.connect(transport); | ||
const request = { method: "example", params: {} }; | ||
try { | ||
const mockSchema: ZodType<{ result: string }> = z.object({ | ||
result: z.string(), | ||
}); | ||
await protocol.request(request, mockSchema, { | ||
timeout: 0, | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(McpError); | ||
if (error instanceof McpError) { | ||
expect(error.code).toBe(ErrorCode.RequestTimeout); | ||
} | ||
} | ||
}); | ||
|
||
test("should invoke onclose when the connection is closed", async () => { | ||
const oncloseMock = jest.fn(); | ||
protocol.onclose = oncloseMock; | ||
await protocol.connect(transport); | ||
await transport.close(); | ||
expect(oncloseMock).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters