Skip to content

Commit e7d08c7

Browse files
committed
fix(protocol): restrict SDK error codes to JSON-RPC server error range
The SDK's custom error codes were previously using arbitrary negative numbers (-1, -2) which could conflict with application-specific error codes. This change: - Updates ConnectionClosed from -1 to -32000 - Updates RequestTimeout from -2 to -32001 - Keeps standard JSON-RPC error codes unchanged - Adds documentation explaining the server error range usage This modification ensures compliance with the JSON-RPC 2.0 specification, which reserves -32000 to -32099 for implementation-defined server errors. This change allows applications to freely use other error code ranges without potential conflicts with SDK-generated errors. Issue: #85
1 parent 3164da6 commit e7d08c7

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/types.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const BaseRequestParamsSchema = z
2929
*/
3030
progressToken: z.optional(ProgressTokenSchema),
3131
})
32-
.passthrough(),
32+
.passthrough()
3333
),
3434
})
3535
.passthrough();
@@ -100,12 +100,15 @@ export const JSONRPCResponseSchema = z
100100
.strict();
101101

102102
/**
103-
* An incomplete set of error codes that may appear in JSON-RPC responses.
103+
* @author : Sumitesh Naithani
104+
* @link : https://docs.trafficserver.apache.org/en/latest/developer-guide/jsonrpc/jsonrpc-node-errors.en.html#standard-errors
105+
* @description : An incomplete set of error codes that may appear in JSON-RPC responses.
106+
* @note : SDK-specific errors should use the server error range (-32000 to -32099), as per JSON-RPC 2.0 specification.
104107
*/
105108
export enum ErrorCode {
106-
// SDK error codes
107-
ConnectionClosed = -1,
108-
RequestTimeout = -2,
109+
// SDK error codes (using server error range)
110+
ConnectionClosed = -32000,
111+
RequestTimeout = -32001,
109112

110113
// Standard JSON-RPC error codes
111114
ParseError = -32700,
@@ -214,7 +217,7 @@ export const ClientCapabilitiesSchema = z
214217
*/
215218
listChanged: z.optional(z.boolean()),
216219
})
217-
.passthrough(),
220+
.passthrough()
218221
),
219222
})
220223
.passthrough();
@@ -258,7 +261,7 @@ export const ServerCapabilitiesSchema = z
258261
*/
259262
listChanged: z.optional(z.boolean()),
260263
})
261-
.passthrough(),
264+
.passthrough()
262265
),
263266
/**
264267
* Present if the server offers any resources to read.
@@ -276,7 +279,7 @@ export const ServerCapabilitiesSchema = z
276279
*/
277280
listChanged: z.optional(z.boolean()),
278281
})
279-
.passthrough(),
282+
.passthrough()
280283
),
281284
/**
282285
* Present if the server offers any tools to call.
@@ -289,7 +292,7 @@ export const ServerCapabilitiesSchema = z
289292
*/
290293
listChanged: z.optional(z.boolean()),
291294
})
292-
.passthrough(),
295+
.passthrough()
293296
),
294297
})
295298
.passthrough();
@@ -480,7 +483,7 @@ export const ListResourcesResultSchema = PaginatedResultSchema.extend({
480483
export const ListResourceTemplatesRequestSchema = PaginatedRequestSchema.extend(
481484
{
482485
method: z.literal("resources/templates/list"),
483-
},
486+
}
484487
);
485488

486489
/**
@@ -508,7 +511,7 @@ export const ReadResourceRequestSchema = RequestSchema.extend({
508511
*/
509512
export const ReadResourceResultSchema = ResultSchema.extend({
510513
contents: z.array(
511-
z.union([TextResourceContentsSchema, BlobResourceContentsSchema]),
514+
z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
512515
),
513516
});
514517

@@ -747,7 +750,7 @@ export const ListToolsResultSchema = PaginatedResultSchema.extend({
747750
*/
748751
export const CallToolResultSchema = ResultSchema.extend({
749752
content: z.array(
750-
z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema]),
753+
z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
751754
),
752755
isError: z.boolean().default(false).optional(),
753756
});
@@ -758,7 +761,7 @@ export const CallToolResultSchema = ResultSchema.extend({
758761
export const CompatibilityCallToolResultSchema = CallToolResultSchema.or(
759762
ResultSchema.extend({
760763
toolResult: z.unknown(),
761-
}),
764+
})
762765
);
763766

764767
/**
@@ -919,7 +922,7 @@ export const CreateMessageResultSchema = ResultSchema.extend({
919922
* The reason why sampling stopped.
920923
*/
921924
stopReason: z.optional(
922-
z.enum(["endTurn", "stopSequence", "maxTokens"]).or(z.string()),
925+
z.enum(["endTurn", "stopSequence", "maxTokens"]).or(z.string())
923926
),
924927
role: z.enum(["user", "assistant"]),
925928
content: z.discriminatedUnion("type", [
@@ -1104,7 +1107,7 @@ export class McpError extends Error {
11041107
constructor(
11051108
public readonly code: number,
11061109
message: string,
1107-
public readonly data?: unknown,
1110+
public readonly data?: unknown
11081111
) {
11091112
super(`MCP error ${code}: ${message}`);
11101113
}

0 commit comments

Comments
 (0)