Skip to content

Commit 0e76d44

Browse files
authored
Merge branch 'main' into fix/module-format
2 parents d207cc4 + 3f178b2 commit 0e76d44

File tree

5 files changed

+92
-14
lines changed

5 files changed

+92
-14
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ npm install @modelcontextprotocol/sdk
2424
```typescript
2525
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2626
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
27+
import {
28+
ListResourcesRequestSchema,
29+
ReadResourceRequestSchema,
30+
} from "@modelcontextprotocol/sdk/types.js";
2731

2832
const transport = new StdioClientTransport({
2933
command: "path/to/server",
@@ -61,6 +65,10 @@ const resourceContent = await client.request(
6165
```typescript
6266
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
6367
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
68+
import {
69+
ListResourcesRequestSchema,
70+
ReadResourceRequestSchema,
71+
} from "@modelcontextprotocol/sdk/types.js";
6472

6573
const server = new Server({
6674
name: "example-server",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@modelcontextprotocol/sdk",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Model Context Protocol implementation for TypeScript",
55
"license": "MIT",
66
"author": "Anthropic, PBC (https://anthropic.com)",

src/client/sse.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export class SSEClientTransport implements Transport {
1212
private _endpoint?: URL;
1313
private _abortController?: AbortController;
1414
private _url: URL;
15+
private _eventSourceInit?: EventSourceInit;
16+
private _requestInit?: RequestInit;
1517

1618
onclose?: () => void;
1719
onerror?: (error: Error) => void;
1820
onmessage?: (message: JSONRPCMessage) => void;
1921

20-
constructor(url: URL) {
22+
constructor(url: URL, opts?: { eventSourceInit?: EventSourceInit, requestInit?: RequestInit }) {
2123
this._url = url;
24+
this._eventSourceInit = opts?.eventSourceInit;
25+
this._requestInit = opts?.requestInit;
2226
}
2327

2428
start(): Promise<void> {
@@ -29,7 +33,7 @@ export class SSEClientTransport implements Transport {
2933
}
3034

3135
return new Promise((resolve, reject) => {
32-
this._eventSource = new EventSource(this._url.href);
36+
this._eventSource = new EventSource(this._url.href, this._eventSourceInit);
3337
this._abortController = new AbortController();
3438

3539
this._eventSource.onerror = (event) => {
@@ -90,14 +94,17 @@ export class SSEClientTransport implements Transport {
9094
}
9195

9296
try {
93-
const response = await fetch(this._endpoint, {
97+
const headers = new Headers(this._requestInit?.headers);
98+
headers.set("content-type", "application/json");
99+
const init = {
100+
...this._requestInit,
94101
method: "POST",
95-
headers: {
96-
"Content-Type": "application/json",
97-
},
102+
headers,
98103
body: JSON.stringify(message),
99-
signal: this._abortController?.signal,
100-
});
104+
signal: this._abortController?.signal
105+
};
106+
107+
const response = await fetch(this._endpoint, init);
101108

102109
if (!response.ok) {
103110
const text = await response.text().catch(() => null);

src/shared/protocol.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Protocol } from "./protocol.js";
2+
import { Transport } from "./transport.js";
3+
import {
4+
McpError,
5+
ErrorCode,
6+
Request,
7+
Result,
8+
Notification,
9+
} from "../types.js";
10+
import { ZodType, z } from "zod";
11+
12+
// Mock Transport class
13+
class MockTransport implements Transport {
14+
onclose?: () => void;
15+
onerror?: (error: Error) => void;
16+
onmessage?: (message: unknown) => void;
17+
18+
async start(): Promise<void> {}
19+
async close(): Promise<void> {
20+
this.onclose?.();
21+
}
22+
async send(_message: unknown): Promise<void> {}
23+
}
24+
25+
describe("protocol tests", () => {
26+
let protocol: Protocol<Request, Notification, Result>;
27+
let transport: MockTransport;
28+
29+
beforeEach(() => {
30+
transport = new MockTransport();
31+
protocol = new (class extends Protocol<Request, Notification, Result> {
32+
protected assertCapabilityForMethod(): void {}
33+
protected assertNotificationCapability(): void {}
34+
protected assertRequestHandlerCapability(): void {}
35+
})();
36+
});
37+
38+
test("should throw a timeout error if the request exceeds the timeout", async () => {
39+
await protocol.connect(transport);
40+
const request = { method: "example", params: {} };
41+
try {
42+
const mockSchema: ZodType<{ result: string }> = z.object({
43+
result: z.string(),
44+
});
45+
await protocol.request(request, mockSchema, {
46+
timeout: 0,
47+
});
48+
} catch (error) {
49+
expect(error).toBeInstanceOf(McpError);
50+
if (error instanceof McpError) {
51+
expect(error.code).toBe(ErrorCode.RequestTimeout);
52+
}
53+
}
54+
});
55+
56+
test("should invoke onclose when the connection is closed", async () => {
57+
const oncloseMock = jest.fn();
58+
protocol.onclose = oncloseMock;
59+
await protocol.connect(transport);
60+
await transport.close();
61+
expect(oncloseMock).toHaveBeenCalled();
62+
});
63+
});

src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ export const JSONRPCResponseSchema = z
100100
.strict();
101101

102102
/**
103-
* An incomplete set of error codes that may appear in JSON-RPC responses.
103+
* Error codes defined by the JSON-RPC specification.
104104
*/
105105
export enum ErrorCode {
106106
// SDK error codes
107-
ConnectionClosed = -1,
108-
RequestTimeout = -2,
109-
107+
ConnectionClosed = -32000,
108+
RequestTimeout = -32001,
109+
110110
// Standard JSON-RPC error codes
111111
ParseError = -32700,
112112
InvalidRequest = -32600,
@@ -1237,4 +1237,4 @@ export type ClientResult = z.infer<typeof ClientResultSchema>;
12371237
/* Server messages */
12381238
export type ServerRequest = z.infer<typeof ServerRequestSchema>;
12391239
export type ServerNotification = z.infer<typeof ServerNotificationSchema>;
1240-
export type ServerResult = z.infer<typeof ServerResultSchema>;
1240+
export type ServerResult = z.infer<typeof ServerResultSchema>;

0 commit comments

Comments
 (0)