Skip to content

Commit cd6820e

Browse files
committed
Use an enum for error codes
1 parent 4be31d0 commit cd6820e

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/shared/protocol.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {
2-
CONNECTION_CLOSED_ERROR,
3-
INTERNAL_ERROR,
2+
ErrorCode,
43
JSONRPCError,
54
JSONRPCNotification,
65
JSONRPCRequest,
76
JSONRPCResponse,
87
McpError,
9-
METHOD_NOT_FOUND,
108
Notification,
119
PingRequestSchema,
1210
Progress,
@@ -124,7 +122,7 @@ export class Protocol<
124122
this._transport = undefined;
125123
this.onclose?.();
126124

127-
const error = new McpError(CONNECTION_CLOSED_ERROR, "Connection closed");
125+
const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
128126
for (const handler of responseHandlers.values()) {
129127
handler(error);
130128
}
@@ -161,7 +159,7 @@ export class Protocol<
161159
jsonrpc: "2.0",
162160
id: request.id,
163161
error: {
164-
code: METHOD_NOT_FOUND,
162+
code: ErrorCode.MethodNotFound,
165163
message: "Method not found",
166164
},
167165
})
@@ -189,7 +187,7 @@ export class Protocol<
189187
error: {
190188
code: error["code"]
191189
? Math.floor(Number(error["code"]))
192-
: INTERNAL_ERROR,
190+
: ErrorCode.InternalError,
193191
message: error.message ?? "Internal error",
194192
},
195193
});

src/types.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@ export const JSONRPCResponseSchema = z
8282
})
8383
.strict();
8484

85-
// Standard JSON-RPC error codes
86-
export const PARSE_ERROR = -32700;
87-
export const INVALID_REQUEST = -32600;
88-
export const METHOD_NOT_FOUND = -32601;
89-
export const INVALID_PARAMS = -32602;
90-
export const INTERNAL_ERROR = -32603;
85+
/**
86+
* An incomplete set of error codes that may appear in JSON-RPC responses.
87+
*/
88+
export enum ErrorCode {
89+
// SDK error codes
90+
ConnectionClosed = -1,
91+
92+
// Standard JSON-RPC error codes
93+
ParseError = -32700,
94+
InvalidRequest = -32600,
95+
MethodNotFound = -32601,
96+
InvalidParams = -32602,
97+
InternalError = -32603,
98+
}
9199

92100
/**
93101
* A response to a request that indicates an error occurred.
@@ -802,8 +810,6 @@ export const ServerResultSchema = z.union([
802810
ListToolsResultSchema,
803811
]);
804812

805-
export const CONNECTION_CLOSED_ERROR = -1;
806-
807813
export class McpError extends Error {
808814
constructor(
809815
public readonly code: number,

0 commit comments

Comments
 (0)