@modelcontextprotocol/sdk 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli.js +13 -0
- package/dist/cli.js.map +1 -1
- package/dist/client/index.d.ts +23 -12
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +133 -37
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.test.js +301 -3
- package/dist/client/index.test.js.map +1 -1
- package/dist/client/stdio.d.ts +7 -0
- package/dist/client/stdio.d.ts.map +1 -1
- package/dist/client/stdio.js +6 -5
- package/dist/client/stdio.js.map +1 -1
- package/dist/inMemory.d.ts +20 -0
- package/dist/inMemory.d.ts.map +1 -0
- package/dist/inMemory.js +47 -0
- package/dist/inMemory.js.map +1 -0
- package/dist/inMemory.test.d.ts +2 -0
- package/dist/inMemory.test.d.ts.map +1 -0
- package/dist/inMemory.test.js +74 -0
- package/dist/inMemory.test.js.map +1 -0
- package/dist/server/index.d.ts +15 -5
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +99 -23
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.test.js +310 -3
- package/dist/server/index.test.js.map +1 -1
- package/dist/shared/protocol.d.ts +64 -7
- package/dist/shared/protocol.d.ts.map +1 -1
- package/dist/shared/protocol.js +60 -11
- package/dist/shared/protocol.js.map +1 -1
- package/dist/types.d.ts +753 -362
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +55 -26
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +5 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +11 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.test.d.ts +2 -0
- package/dist/utils.test.d.ts.map +1 -0
- package/dist/utils.test.js +14 -0
- package/dist/utils.test.js.map +1 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,eAAe,EACf,cAAc,EACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EAGpB,cAAc,EAMd,gBAAgB,EAEhB,0BAA0B,EAC1B,YAAY,EACZ,OAAO,EACP,2BAA2B,EAC3B,MAAM,EACN,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,YAAY,EAEb,MAAM,aAAa,CAAC;AAErB,MAAM,MAAM,aAAa,GAAG,eAAe,GAAG;IAC5C;;OAEG;IACH,YAAY,EAAE,kBAAkB,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,MAAM,CACjB,QAAQ,SAAS,OAAO,GAAG,OAAO,EAClC,aAAa,SAAS,YAAY,GAAG,YAAY,EACjD,OAAO,SAAS,MAAM,GAAG,MAAM,CAC/B,SAAQ,QAAQ,CAChB,aAAa,GAAG,QAAQ,EACxB,kBAAkB,GAAG,aAAa,EAClC,YAAY,GAAG,OAAO,CACvB;IAcG,OAAO,CAAC,WAAW;IAbrB,OAAO,CAAC,mBAAmB,CAAC,CAAqB;IACjD,OAAO,CAAC,cAAc,CAAC,CAAiB;IACxC,OAAO,CAAC,aAAa,CAAqB;IAE1C;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,IAAI,CAAC;IAE3B;;OAEG;gBAEO,WAAW,EAAE,cAAc,EACnC,OAAO,EAAE,aAAa;IAaxB,SAAS,CAAC,yBAAyB,CAAC,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,GAAG,IAAI;IAwBrE,SAAS,CAAC,4BAA4B,CACpC,MAAM,EAAE,CAAC,kBAAkB,GAAG,aAAa,CAAC,CAAC,QAAQ,CAAC,GACrD,IAAI;IA6CP,SAAS,CAAC,8BAA8B,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;YAqDhD,aAAa;IAiB3B;;OAEG;IACH,qBAAqB,IAAI,kBAAkB,GAAG,SAAS;IAIvD;;OAEG;IACH,gBAAgB,IAAI,cAAc,GAAG,SAAS;IAI9C,OAAO,CAAC,eAAe;IAIjB,IAAI;;;IAIJ,aAAa,CACjB,MAAM,EAAE,oBAAoB,CAAC,QAAQ,CAAC,EACtC,OAAO,CAAC,EAAE,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IASpB,SAAS,CACb,MAAM,CAAC,EAAE,gBAAgB,CAAC,QAAQ,CAAC,EACnC,OAAO,CAAC,EAAE,cAAc;;;;;;;;;;;;;;IASpB,kBAAkB,CAAC,MAAM,EAAE,0BAA0B,CAAC,QAAQ,CAAC;IAI/D,mBAAmB,CAAC,MAAM,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAOjE,uBAAuB;IAMvB,mBAAmB;IAInB,qBAAqB;CAG5B"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Protocol } from "../shared/protocol.js";
|
|
2
|
-
import { CreateMessageResultSchema, EmptyResultSchema, InitializedNotificationSchema, InitializeRequestSchema, LATEST_PROTOCOL_VERSION,
|
|
1
|
+
import { Protocol, } from "../shared/protocol.js";
|
|
2
|
+
import { CreateMessageResultSchema, EmptyResultSchema, InitializedNotificationSchema, InitializeRequestSchema, LATEST_PROTOCOL_VERSION, ListRootsResultSchema, SUPPORTED_PROTOCOL_VERSIONS, } from "../types.js";
|
|
3
3
|
/**
|
|
4
4
|
* An MCP server on top of a pluggable transport.
|
|
5
5
|
*
|
|
@@ -29,18 +29,107 @@ export class Server extends Protocol {
|
|
|
29
29
|
/**
|
|
30
30
|
* Initializes this server with the given name and version information.
|
|
31
31
|
*/
|
|
32
|
-
constructor(_serverInfo) {
|
|
33
|
-
super();
|
|
32
|
+
constructor(_serverInfo, options) {
|
|
33
|
+
super(options);
|
|
34
34
|
this._serverInfo = _serverInfo;
|
|
35
|
+
this._capabilities = options.capabilities;
|
|
35
36
|
this.setRequestHandler(InitializeRequestSchema, (request) => this._oninitialize(request));
|
|
36
37
|
this.setNotificationHandler(InitializedNotificationSchema, () => { var _a; return (_a = this.oninitialized) === null || _a === void 0 ? void 0 : _a.call(this); });
|
|
37
38
|
}
|
|
39
|
+
assertCapabilityForMethod(method) {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
switch (method) {
|
|
42
|
+
case "sampling/createMessage":
|
|
43
|
+
if (!((_a = this._clientCapabilities) === null || _a === void 0 ? void 0 : _a.sampling)) {
|
|
44
|
+
throw new Error(`Client does not support sampling (required for ${method})`);
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
case "roots/list":
|
|
48
|
+
if (!((_b = this._clientCapabilities) === null || _b === void 0 ? void 0 : _b.roots)) {
|
|
49
|
+
throw new Error(`Client does not support listing roots (required for ${method})`);
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case "ping":
|
|
53
|
+
// No specific capability required for ping
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
assertNotificationCapability(method) {
|
|
58
|
+
switch (method) {
|
|
59
|
+
case "notifications/message":
|
|
60
|
+
if (!this._capabilities.logging) {
|
|
61
|
+
throw new Error(`Server does not support logging (required for ${method})`);
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case "notifications/resources/updated":
|
|
65
|
+
case "notifications/resources/list_changed":
|
|
66
|
+
if (!this._capabilities.resources) {
|
|
67
|
+
throw new Error(`Server does not support notifying about resources (required for ${method})`);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
case "notifications/tools/list_changed":
|
|
71
|
+
if (!this._capabilities.tools) {
|
|
72
|
+
throw new Error(`Server does not support notifying of tool list changes (required for ${method})`);
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "notifications/prompts/list_changed":
|
|
76
|
+
if (!this._capabilities.prompts) {
|
|
77
|
+
throw new Error(`Server does not support notifying of prompt list changes (required for ${method})`);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case "notifications/cancelled":
|
|
81
|
+
// Cancellation notifications are always allowed
|
|
82
|
+
break;
|
|
83
|
+
case "notifications/progress":
|
|
84
|
+
// Progress notifications are always allowed
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
assertRequestHandlerCapability(method) {
|
|
89
|
+
switch (method) {
|
|
90
|
+
case "sampling/createMessage":
|
|
91
|
+
if (!this._capabilities.sampling) {
|
|
92
|
+
throw new Error(`Server does not support sampling (required for ${method})`);
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "logging/setLevel":
|
|
96
|
+
if (!this._capabilities.logging) {
|
|
97
|
+
throw new Error(`Server does not support logging (required for ${method})`);
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case "prompts/get":
|
|
101
|
+
case "prompts/list":
|
|
102
|
+
if (!this._capabilities.prompts) {
|
|
103
|
+
throw new Error(`Server does not support prompts (required for ${method})`);
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
case "resources/list":
|
|
107
|
+
case "resources/templates/list":
|
|
108
|
+
case "resources/read":
|
|
109
|
+
if (!this._capabilities.resources) {
|
|
110
|
+
throw new Error(`Server does not support resources (required for ${method})`);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
case "tools/call":
|
|
114
|
+
case "tools/list":
|
|
115
|
+
if (!this._capabilities.tools) {
|
|
116
|
+
throw new Error(`Server does not support tools (required for ${method})`);
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
case "ping":
|
|
120
|
+
case "initialize":
|
|
121
|
+
// No specific capability required for these methods
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
38
125
|
async _oninitialize(request) {
|
|
39
126
|
const requestedVersion = request.params.protocolVersion;
|
|
40
127
|
this._clientCapabilities = request.params.capabilities;
|
|
41
128
|
this._clientVersion = request.params.clientInfo;
|
|
42
129
|
return {
|
|
43
|
-
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion)
|
|
130
|
+
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion)
|
|
131
|
+
? requestedVersion
|
|
132
|
+
: LATEST_PROTOCOL_VERSION,
|
|
44
133
|
capabilities: this.getCapabilities(),
|
|
45
134
|
serverInfo: this._serverInfo,
|
|
46
135
|
};
|
|
@@ -58,29 +147,16 @@ export class Server extends Protocol {
|
|
|
58
147
|
return this._clientVersion;
|
|
59
148
|
}
|
|
60
149
|
getCapabilities() {
|
|
61
|
-
return
|
|
62
|
-
prompts: this._requestHandlers.has(ListPromptsRequestSchema.shape.method.value)
|
|
63
|
-
? {}
|
|
64
|
-
: undefined,
|
|
65
|
-
resources: this._requestHandlers.has(ListResourcesRequestSchema.shape.method.value)
|
|
66
|
-
? {}
|
|
67
|
-
: undefined,
|
|
68
|
-
tools: this._requestHandlers.has(ListToolsRequestSchema.shape.method.value)
|
|
69
|
-
? {}
|
|
70
|
-
: undefined,
|
|
71
|
-
logging: this._requestHandlers.has(SetLevelRequestSchema.shape.method.value)
|
|
72
|
-
? {}
|
|
73
|
-
: undefined,
|
|
74
|
-
};
|
|
150
|
+
return this._capabilities;
|
|
75
151
|
}
|
|
76
152
|
async ping() {
|
|
77
153
|
return this.request({ method: "ping" }, EmptyResultSchema);
|
|
78
154
|
}
|
|
79
|
-
async createMessage(params,
|
|
80
|
-
return this.request({ method: "sampling/createMessage", params }, CreateMessageResultSchema,
|
|
155
|
+
async createMessage(params, options) {
|
|
156
|
+
return this.request({ method: "sampling/createMessage", params }, CreateMessageResultSchema, options);
|
|
81
157
|
}
|
|
82
|
-
async listRoots(params,
|
|
83
|
-
return this.request({ method: "roots/list", params }, ListRootsResultSchema,
|
|
158
|
+
async listRoots(params, options) {
|
|
159
|
+
return this.request({ method: "roots/list", params }, ListRootsResultSchema, options);
|
|
84
160
|
}
|
|
85
161
|
async sendLoggingMessage(params) {
|
|
86
162
|
return this.notification({ method: "notifications/message", params });
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,GAGT,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAGL,yBAAyB,EACzB,iBAAiB,EAEjB,6BAA6B,EAE7B,uBAAuB,EAEvB,uBAAuB,EAEvB,qBAAqB,EAUrB,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AASrB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,MAIX,SAAQ,QAIT;IAUC;;OAEG;IACH,YACU,WAA2B,EACnC,OAAsB;QAEtB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHP,gBAAW,GAAX,WAAW,CAAgB;QAInC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;QAE1C,IAAI,CAAC,iBAAiB,CAAC,uBAAuB,EAAE,CAAC,OAAO,EAAE,EAAE,CAC1D,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAC5B,CAAC;QACF,IAAI,CAAC,sBAAsB,CAAC,6BAA6B,EAAE,GAAG,EAAE,WAC9D,OAAA,MAAA,IAAI,CAAC,aAAa,oDAAI,CAAA,EAAA,CACvB,CAAC;IACJ,CAAC;IAES,yBAAyB,CAAC,MAA0B;;QAC5D,QAAQ,MAAiC,EAAE,CAAC;YAC1C,KAAK,wBAAwB;gBAC3B,IAAI,CAAC,CAAA,MAAA,IAAI,CAAC,mBAAmB,0CAAE,QAAQ,CAAA,EAAE,CAAC;oBACxC,MAAM,IAAI,KAAK,CACb,kDAAkD,MAAM,GAAG,CAC5D,CAAC;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IACL,CAAC;IAED,KAAK,CAAC,uBAAuB;QAC3B,OAAO,IAAI,CAAC,YAAY,CAAC;YACvB,MAAM,EAAE,sCAAsC;SAC/C,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,mBAAmB;QACvB,OAAO,IAAI,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,kCAAkC,EAAE,CAAC,CAAC;IAC3E,CAAC;IAED,KAAK,CAAC,qBAAqB;QACzB,OAAO,IAAI,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,oCAAoC,EAAE,CAAC,CAAC;IAC7E,CAAC;CACF"}
|
|
@@ -3,10 +3,263 @@
|
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
4
4
|
import { Server } from "./index.js";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
|
|
6
|
+
import { RequestSchema, NotificationSchema, ResultSchema, LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS, CreateMessageRequestSchema, ListPromptsRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, SetLevelRequestSchema, } from "../types.js";
|
|
7
|
+
import { InMemoryTransport } from "../inMemory.js";
|
|
8
|
+
import { Client } from "../client/index.js";
|
|
9
|
+
test("should accept latest protocol version", async () => {
|
|
10
|
+
var _a;
|
|
11
|
+
let sendPromiseResolve;
|
|
12
|
+
const sendPromise = new Promise((resolve) => {
|
|
13
|
+
sendPromiseResolve = resolve;
|
|
14
|
+
});
|
|
15
|
+
const serverTransport = {
|
|
16
|
+
start: jest.fn().mockResolvedValue(undefined),
|
|
17
|
+
close: jest.fn().mockResolvedValue(undefined),
|
|
18
|
+
send: jest.fn().mockImplementation((message) => {
|
|
19
|
+
if (message.id === 1 && message.result) {
|
|
20
|
+
expect(message.result).toEqual({
|
|
21
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
22
|
+
capabilities: expect.any(Object),
|
|
23
|
+
serverInfo: {
|
|
24
|
+
name: "test server",
|
|
25
|
+
version: "1.0",
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
sendPromiseResolve(undefined);
|
|
29
|
+
}
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
const server = new Server({
|
|
34
|
+
name: "test server",
|
|
35
|
+
version: "1.0",
|
|
36
|
+
}, {
|
|
37
|
+
capabilities: {
|
|
38
|
+
prompts: {},
|
|
39
|
+
resources: {},
|
|
40
|
+
tools: {},
|
|
41
|
+
logging: {},
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
await server.connect(serverTransport);
|
|
45
|
+
// Simulate initialize request with latest version
|
|
46
|
+
(_a = serverTransport.onmessage) === null || _a === void 0 ? void 0 : _a.call(serverTransport, {
|
|
47
|
+
jsonrpc: "2.0",
|
|
48
|
+
id: 1,
|
|
49
|
+
method: "initialize",
|
|
50
|
+
params: {
|
|
51
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
52
|
+
capabilities: {},
|
|
53
|
+
clientInfo: {
|
|
54
|
+
name: "test client",
|
|
55
|
+
version: "1.0",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
await expect(sendPromise).resolves.toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
test("should accept supported older protocol version", async () => {
|
|
62
|
+
var _a;
|
|
63
|
+
const OLD_VERSION = SUPPORTED_PROTOCOL_VERSIONS[1];
|
|
64
|
+
let sendPromiseResolve;
|
|
65
|
+
const sendPromise = new Promise((resolve) => {
|
|
66
|
+
sendPromiseResolve = resolve;
|
|
67
|
+
});
|
|
68
|
+
const serverTransport = {
|
|
69
|
+
start: jest.fn().mockResolvedValue(undefined),
|
|
70
|
+
close: jest.fn().mockResolvedValue(undefined),
|
|
71
|
+
send: jest.fn().mockImplementation((message) => {
|
|
72
|
+
if (message.id === 1 && message.result) {
|
|
73
|
+
expect(message.result).toEqual({
|
|
74
|
+
protocolVersion: OLD_VERSION,
|
|
75
|
+
capabilities: expect.any(Object),
|
|
76
|
+
serverInfo: {
|
|
77
|
+
name: "test server",
|
|
78
|
+
version: "1.0",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
sendPromiseResolve(undefined);
|
|
82
|
+
}
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}),
|
|
85
|
+
};
|
|
86
|
+
const server = new Server({
|
|
87
|
+
name: "test server",
|
|
88
|
+
version: "1.0",
|
|
89
|
+
}, {
|
|
90
|
+
capabilities: {
|
|
91
|
+
prompts: {},
|
|
92
|
+
resources: {},
|
|
93
|
+
tools: {},
|
|
94
|
+
logging: {},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
await server.connect(serverTransport);
|
|
98
|
+
// Simulate initialize request with older version
|
|
99
|
+
(_a = serverTransport.onmessage) === null || _a === void 0 ? void 0 : _a.call(serverTransport, {
|
|
100
|
+
jsonrpc: "2.0",
|
|
101
|
+
id: 1,
|
|
102
|
+
method: "initialize",
|
|
103
|
+
params: {
|
|
104
|
+
protocolVersion: OLD_VERSION,
|
|
105
|
+
capabilities: {},
|
|
106
|
+
clientInfo: {
|
|
107
|
+
name: "test client",
|
|
108
|
+
version: "1.0",
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
await expect(sendPromise).resolves.toBeUndefined();
|
|
113
|
+
});
|
|
114
|
+
test("should handle unsupported protocol version", async () => {
|
|
115
|
+
var _a;
|
|
116
|
+
let sendPromiseResolve;
|
|
117
|
+
const sendPromise = new Promise((resolve) => {
|
|
118
|
+
sendPromiseResolve = resolve;
|
|
119
|
+
});
|
|
120
|
+
const serverTransport = {
|
|
121
|
+
start: jest.fn().mockResolvedValue(undefined),
|
|
122
|
+
close: jest.fn().mockResolvedValue(undefined),
|
|
123
|
+
send: jest.fn().mockImplementation((message) => {
|
|
124
|
+
if (message.id === 1 && message.result) {
|
|
125
|
+
expect(message.result).toEqual({
|
|
126
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
127
|
+
capabilities: expect.any(Object),
|
|
128
|
+
serverInfo: {
|
|
129
|
+
name: "test server",
|
|
130
|
+
version: "1.0",
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
sendPromiseResolve(undefined);
|
|
134
|
+
}
|
|
135
|
+
return Promise.resolve();
|
|
136
|
+
}),
|
|
137
|
+
};
|
|
138
|
+
const server = new Server({
|
|
139
|
+
name: "test server",
|
|
140
|
+
version: "1.0",
|
|
141
|
+
}, {
|
|
142
|
+
capabilities: {
|
|
143
|
+
prompts: {},
|
|
144
|
+
resources: {},
|
|
145
|
+
tools: {},
|
|
146
|
+
logging: {},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
await server.connect(serverTransport);
|
|
150
|
+
// Simulate initialize request with unsupported version
|
|
151
|
+
(_a = serverTransport.onmessage) === null || _a === void 0 ? void 0 : _a.call(serverTransport, {
|
|
152
|
+
jsonrpc: "2.0",
|
|
153
|
+
id: 1,
|
|
154
|
+
method: "initialize",
|
|
155
|
+
params: {
|
|
156
|
+
protocolVersion: "invalid-version",
|
|
157
|
+
capabilities: {},
|
|
158
|
+
clientInfo: {
|
|
159
|
+
name: "test client",
|
|
160
|
+
version: "1.0",
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
await expect(sendPromise).resolves.toBeUndefined();
|
|
165
|
+
});
|
|
166
|
+
test("should respect client capabilities", async () => {
|
|
167
|
+
const server = new Server({
|
|
168
|
+
name: "test server",
|
|
169
|
+
version: "1.0",
|
|
170
|
+
}, {
|
|
171
|
+
capabilities: {
|
|
172
|
+
prompts: {},
|
|
173
|
+
resources: {},
|
|
174
|
+
tools: {},
|
|
175
|
+
logging: {},
|
|
176
|
+
},
|
|
177
|
+
enforceStrictCapabilities: true,
|
|
178
|
+
});
|
|
179
|
+
const client = new Client({
|
|
180
|
+
name: "test client",
|
|
181
|
+
version: "1.0",
|
|
182
|
+
}, {
|
|
183
|
+
capabilities: {
|
|
184
|
+
sampling: {},
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
// Implement request handler for sampling/createMessage
|
|
188
|
+
client.setRequestHandler(CreateMessageRequestSchema, async (request) => {
|
|
189
|
+
// Mock implementation of createMessage
|
|
190
|
+
return {
|
|
191
|
+
model: "test-model",
|
|
192
|
+
role: "assistant",
|
|
193
|
+
content: {
|
|
194
|
+
type: "text",
|
|
195
|
+
text: "This is a test response",
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
200
|
+
await Promise.all([
|
|
201
|
+
client.connect(clientTransport),
|
|
202
|
+
server.connect(serverTransport),
|
|
203
|
+
]);
|
|
204
|
+
expect(server.getClientCapabilities()).toEqual({ sampling: {} });
|
|
205
|
+
// This should work because sampling is supported by the client
|
|
206
|
+
await expect(server.createMessage({
|
|
207
|
+
messages: [],
|
|
208
|
+
maxTokens: 10,
|
|
209
|
+
})).resolves.not.toThrow();
|
|
210
|
+
// This should still throw because roots are not supported by the client
|
|
211
|
+
await expect(server.listRoots()).rejects.toThrow(/^Client does not support/);
|
|
212
|
+
});
|
|
213
|
+
test("should respect server notification capabilities", async () => {
|
|
214
|
+
const server = new Server({
|
|
215
|
+
name: "test server",
|
|
216
|
+
version: "1.0",
|
|
217
|
+
}, {
|
|
218
|
+
capabilities: {
|
|
219
|
+
logging: {},
|
|
220
|
+
},
|
|
221
|
+
enforceStrictCapabilities: true,
|
|
222
|
+
});
|
|
223
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
224
|
+
await server.connect(serverTransport);
|
|
225
|
+
// This should work because logging is supported by the server
|
|
226
|
+
await expect(server.sendLoggingMessage({
|
|
227
|
+
level: "info",
|
|
228
|
+
data: "Test log message",
|
|
229
|
+
})).resolves.not.toThrow();
|
|
230
|
+
// This should throw because resource notificaitons are not supported by the server
|
|
231
|
+
await expect(server.sendResourceUpdated({ uri: "test://resource" })).rejects.toThrow(/^Server does not support/);
|
|
232
|
+
});
|
|
233
|
+
test("should only allow setRequestHandler for declared capabilities", () => {
|
|
234
|
+
const server = new Server({
|
|
235
|
+
name: "test server",
|
|
236
|
+
version: "1.0",
|
|
237
|
+
}, {
|
|
238
|
+
capabilities: {
|
|
239
|
+
prompts: {},
|
|
240
|
+
resources: {},
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
// These should work because the capabilities are declared
|
|
244
|
+
expect(() => {
|
|
245
|
+
server.setRequestHandler(ListPromptsRequestSchema, () => ({ prompts: [] }));
|
|
246
|
+
}).not.toThrow();
|
|
247
|
+
expect(() => {
|
|
248
|
+
server.setRequestHandler(ListResourcesRequestSchema, () => ({
|
|
249
|
+
resources: [],
|
|
250
|
+
}));
|
|
251
|
+
}).not.toThrow();
|
|
252
|
+
// These should throw because the capabilities are not declared
|
|
253
|
+
expect(() => {
|
|
254
|
+
server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: [] }));
|
|
255
|
+
}).toThrow(/^Server does not support tools/);
|
|
256
|
+
expect(() => {
|
|
257
|
+
server.setRequestHandler(SetLevelRequestSchema, () => ({}));
|
|
258
|
+
}).toThrow(/^Server does not support logging/);
|
|
259
|
+
});
|
|
7
260
|
/*
|
|
8
|
-
Test that custom request/notification/result schemas can be used with the Server class.
|
|
9
|
-
*/
|
|
261
|
+
Test that custom request/notification/result schemas can be used with the Server class.
|
|
262
|
+
*/
|
|
10
263
|
test("should typecheck", () => {
|
|
11
264
|
const GetWeatherRequestSchema = RequestSchema.extend({
|
|
12
265
|
method: z.literal("weather/get"),
|
|
@@ -38,6 +291,13 @@ test("should typecheck", () => {
|
|
|
38
291
|
const weatherServer = new Server({
|
|
39
292
|
name: "WeatherServer",
|
|
40
293
|
version: "1.0.0",
|
|
294
|
+
}, {
|
|
295
|
+
capabilities: {
|
|
296
|
+
prompts: {},
|
|
297
|
+
resources: {},
|
|
298
|
+
tools: {},
|
|
299
|
+
logging: {},
|
|
300
|
+
},
|
|
41
301
|
});
|
|
42
302
|
// Typecheck that only valid weather requests/notifications/results are allowed
|
|
43
303
|
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {
|
|
@@ -50,4 +310,51 @@ test("should typecheck", () => {
|
|
|
50
310
|
console.log(`Weather alert: ${notification.params.message}`);
|
|
51
311
|
});
|
|
52
312
|
});
|
|
313
|
+
test("should handle server cancelling a request", async () => {
|
|
314
|
+
const server = new Server({
|
|
315
|
+
name: "test server",
|
|
316
|
+
version: "1.0",
|
|
317
|
+
}, {
|
|
318
|
+
capabilities: {
|
|
319
|
+
sampling: {},
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
const client = new Client({
|
|
323
|
+
name: "test client",
|
|
324
|
+
version: "1.0",
|
|
325
|
+
}, {
|
|
326
|
+
capabilities: {
|
|
327
|
+
sampling: {},
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
// Set up client to delay responding to createMessage
|
|
331
|
+
client.setRequestHandler(CreateMessageRequestSchema, async (_request, extra) => {
|
|
332
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
333
|
+
return {
|
|
334
|
+
model: "test",
|
|
335
|
+
role: "assistant",
|
|
336
|
+
content: {
|
|
337
|
+
type: "text",
|
|
338
|
+
text: "Test response",
|
|
339
|
+
},
|
|
340
|
+
};
|
|
341
|
+
});
|
|
342
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
343
|
+
await Promise.all([
|
|
344
|
+
client.connect(clientTransport),
|
|
345
|
+
server.connect(serverTransport),
|
|
346
|
+
]);
|
|
347
|
+
// Set up abort controller
|
|
348
|
+
const controller = new AbortController();
|
|
349
|
+
// Issue request but cancel it immediately
|
|
350
|
+
const createMessagePromise = server.createMessage({
|
|
351
|
+
messages: [],
|
|
352
|
+
maxTokens: 10,
|
|
353
|
+
}, {
|
|
354
|
+
signal: controller.signal,
|
|
355
|
+
});
|
|
356
|
+
controller.abort("Cancelled by test");
|
|
357
|
+
// Request should be rejected
|
|
358
|
+
await expect(createMessagePromise).rejects.toBe("Cancelled by test");
|
|
359
|
+
});
|
|
53
360
|
//# sourceMappingURL=index.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/server/index.test.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,kDAAkD;AAClD,6DAA6D;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/server/index.test.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,kDAAkD;AAClD,6DAA6D;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,uBAAuB,EACvB,2BAA2B,EAC3B,0BAA0B,EAC1B,wBAAwB,EACxB,0BAA0B,EAC1B,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,IAAI,CAAC,uCAAuC,EAAE,KAAK,IAAI,EAAE;;IACvD,IAAI,kBAA4C,CAAC;IACjD,MAAM,WAAW,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC1C,kBAAkB,GAAG,OAAO,CAAC;IAC/B,CAAC,CAAC,CAAC;IAEH,MAAM,eAAe,GAAc;QACjC,KAAK,EAAE,IAAI,CAAC,EAAE,EAA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AM,CAAC,iBAAiB,CACtB,0BAA0B,EAC1B,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;QACxB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;QAC1D,OAAO;YACL,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE;gBACP,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,eAAe;aACtB;SACF,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,MAAM,CAAC,eAAe,EAAE,eAAe,CAAC,GACtC,iBAAiB,CAAC,gBAAgB,EAAE,CAAC;IAEvC,MAAM,OAAO,CAAC,GAAG,CAAC;QAChB,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;QAC/B,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;KAChC,CAAC,CAAC;IAEH,0BAA0B;IAC1B,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IAEzC,0CAA0C;IAC1C,MAAM,oBAAoB,GAAG,MAAM,CAAC,aAAa,CAC/C;QACE,QAAQ,EAAE,EAAE;QACZ,SAAS,EAAE,EAAE;KACd,EACD;QACE,MAAM,EAAE,UAAU,CAAC,MAAM;KAC1B,CACF,CAAC;IACF,UAAU,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAEtC,6BAA6B;IAC7B,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;AACvE,CAAC,CAAC,CAAC"}
|
|
@@ -1,18 +1,57 @@
|
|
|
1
1
|
import { ZodLiteral, ZodObject, ZodType, z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { Notification, Progress, Request, Result } from "../types.js";
|
|
3
3
|
import { Transport } from "./transport.js";
|
|
4
4
|
/**
|
|
5
5
|
* Callback for progress notifications.
|
|
6
6
|
*/
|
|
7
7
|
export type ProgressCallback = (progress: Progress) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Additional initialization options.
|
|
10
|
+
*/
|
|
11
|
+
export type ProtocolOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Whether to restrict emitted requests to only those that the remote side has indicated that they can handle, through their advertised capabilities.
|
|
14
|
+
*
|
|
15
|
+
* Note that this DOES NOT affect checking of _local_ side capabilities, as it is considered a logic error to mis-specify those.
|
|
16
|
+
*
|
|
17
|
+
* Currently this defaults to false, for backwards compatibility with SDK versions that did not advertise capabilities correctly. In future, this will default to true.
|
|
18
|
+
*/
|
|
19
|
+
enforceStrictCapabilities?: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Options that can be given per request.
|
|
23
|
+
*/
|
|
24
|
+
export type RequestOptions = {
|
|
25
|
+
/**
|
|
26
|
+
* If set, requests progress notifications from the remote end (if supported). When progress notifications are received, this callback will be invoked.
|
|
27
|
+
*/
|
|
28
|
+
onprogress?: ProgressCallback;
|
|
29
|
+
/**
|
|
30
|
+
* Can be used to cancel an in-flight request. This will cause an AbortError to be raised from request().
|
|
31
|
+
*
|
|
32
|
+
* Use abortAfterTimeout() to easily implement timeouts using this signal.
|
|
33
|
+
*/
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Extra data given to request handlers.
|
|
38
|
+
*/
|
|
39
|
+
export type RequestHandlerExtra = {
|
|
40
|
+
/**
|
|
41
|
+
* An abort signal used to communicate if the request was cancelled from the sender's side.
|
|
42
|
+
*/
|
|
43
|
+
signal: AbortSignal;
|
|
44
|
+
};
|
|
8
45
|
/**
|
|
9
46
|
* Implements MCP protocol framing on top of a pluggable transport, including
|
|
10
47
|
* features like request/response linking, notifications, and progress.
|
|
11
48
|
*/
|
|
12
|
-
export declare class Protocol<SendRequestT extends Request, SendNotificationT extends Notification, SendResultT extends Result> {
|
|
49
|
+
export declare abstract class Protocol<SendRequestT extends Request, SendNotificationT extends Notification, SendResultT extends Result> {
|
|
50
|
+
private _options?;
|
|
13
51
|
private _transport?;
|
|
14
52
|
private _requestMessageId;
|
|
15
|
-
|
|
53
|
+
private _requestHandlers;
|
|
54
|
+
private _requestHandlerAbortControllers;
|
|
16
55
|
private _notificationHandlers;
|
|
17
56
|
private _responseHandlers;
|
|
18
57
|
private _progressHandlers;
|
|
@@ -36,7 +75,7 @@ export declare class Protocol<SendRequestT extends Request, SendNotificationT ex
|
|
|
36
75
|
* A handler to invoke for any notification types that do not have their own handler installed.
|
|
37
76
|
*/
|
|
38
77
|
fallbackNotificationHandler?: (notification: Notification) => Promise<void>;
|
|
39
|
-
constructor();
|
|
78
|
+
constructor(_options?: ProtocolOptions | undefined);
|
|
40
79
|
/**
|
|
41
80
|
* Attaches to the given transport, starts it, and starts listening for messages.
|
|
42
81
|
*
|
|
@@ -55,11 +94,29 @@ export declare class Protocol<SendRequestT extends Request, SendNotificationT ex
|
|
|
55
94
|
*/
|
|
56
95
|
close(): Promise<void>;
|
|
57
96
|
/**
|
|
58
|
-
*
|
|
97
|
+
* A method to check if a capability is supported by the remote side, for the given method to be called.
|
|
98
|
+
*
|
|
99
|
+
* This should be implemented by subclasses.
|
|
100
|
+
*/
|
|
101
|
+
protected abstract assertCapabilityForMethod(method: SendRequestT["method"]): void;
|
|
102
|
+
/**
|
|
103
|
+
* A method to check if a notification is supported by the local side, for the given method to be sent.
|
|
104
|
+
*
|
|
105
|
+
* This should be implemented by subclasses.
|
|
106
|
+
*/
|
|
107
|
+
protected abstract assertNotificationCapability(method: SendNotificationT["method"]): void;
|
|
108
|
+
/**
|
|
109
|
+
* A method to check if a request handler is supported by the local side, for the given method to be handled.
|
|
110
|
+
*
|
|
111
|
+
* This should be implemented by subclasses.
|
|
112
|
+
*/
|
|
113
|
+
protected abstract assertRequestHandlerCapability(method: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Sends a request and wait for a response.
|
|
59
116
|
*
|
|
60
117
|
* Do not use this method to emit notifications! Use notification() instead.
|
|
61
118
|
*/
|
|
62
|
-
request<T extends ZodType<object>>(request: SendRequestT, resultSchema: T,
|
|
119
|
+
request<T extends ZodType<object>>(request: SendRequestT, resultSchema: T, options?: RequestOptions): Promise<z.infer<T>>;
|
|
63
120
|
/**
|
|
64
121
|
* Emits a notification, which is a one-way message that does not expect a response.
|
|
65
122
|
*/
|
|
@@ -71,7 +128,7 @@ export declare class Protocol<SendRequestT extends Request, SendNotificationT ex
|
|
|
71
128
|
*/
|
|
72
129
|
setRequestHandler<T extends ZodObject<{
|
|
73
130
|
method: ZodLiteral<string>;
|
|
74
|
-
}>>(requestSchema: T, handler: (request: z.infer<T
|
|
131
|
+
}>>(requestSchema: T, handler: (request: z.infer<T>, extra: RequestHandlerExtra) => SendResultT | Promise<SendResultT>): void;
|
|
75
132
|
/**
|
|
76
133
|
* Removes the request handler for the given method.
|
|
77
134
|
*/
|