Skip to content

Commit

Permalink
Merge pull request #103 from sumitesh9/sumiteshn9/fix-error-codes
Browse files Browse the repository at this point in the history
Updates SDK error codes to use JSON-RPC server error range
  • Loading branch information
jspahrsummers authored Jan 3, 2025
2 parents 4658fd0 + ca31913 commit 2fd4e35
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
63 changes: 63 additions & 0 deletions src/shared/protocol.test.ts
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();
});
});
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ export const JSONRPCResponseSchema = z
.strict();

/**
* An incomplete set of error codes that may appear in JSON-RPC responses.
* Error codes defined by the JSON-RPC specification.
*/
export enum ErrorCode {
// SDK error codes
ConnectionClosed = -1,
RequestTimeout = -2,

ConnectionClosed = -32000,
RequestTimeout = -32001,
// Standard JSON-RPC error codes
ParseError = -32700,
InvalidRequest = -32600,
Expand Down Expand Up @@ -1237,4 +1237,4 @@ export type ClientResult = z.infer<typeof ClientResultSchema>;
/* Server messages */
export type ServerRequest = z.infer<typeof ServerRequestSchema>;
export type ServerNotification = z.infer<typeof ServerNotificationSchema>;
export type ServerResult = z.infer<typeof ServerResultSchema>;
export type ServerResult = z.infer<typeof ServerResultSchema>;

0 comments on commit 2fd4e35

Please sign in to comment.