Skip to content

Commit 7ab5880

Browse files
committed
Use explicit capabilities lists for now
1 parent 90e91cd commit 7ab5880

File tree

5 files changed

+179
-83
lines changed

5 files changed

+179
-83
lines changed

src/cli.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ import { StdioServerTransport } from "./server/stdio.js";
1717
import { ListResourcesResultSchema } from "./types.js";
1818

1919
async function runClient(url_or_command: string, args: string[]) {
20-
const client = new Client({
21-
name: "mcp-typescript test client",
22-
version: "0.1.0",
23-
});
20+
const client = new Client(
21+
{
22+
name: "mcp-typescript test client",
23+
version: "0.1.0",
24+
},
25+
{
26+
sampling: {},
27+
},
28+
);
2429

2530
let clientTransport;
2631

@@ -97,10 +102,18 @@ async function runServer(port: number | null) {
97102
console.log(`Server running on http://localhost:${port}/sse`);
98103
});
99104
} else {
100-
const server = new Server({
101-
name: "mcp-typescript test server",
102-
version: "0.1.0",
103-
});
105+
const server = new Server(
106+
{
107+
name: "mcp-typescript test server",
108+
version: "0.1.0",
109+
},
110+
{
111+
prompts: {},
112+
resources: {},
113+
tools: {},
114+
logging: {},
115+
},
116+
);
104117

105118
const transport = new StdioServerTransport();
106119
await server.connect(transport);

src/client/index.test.ts

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ test("should initialize with matching protocol version", async () => {
4040
}),
4141
};
4242

43-
const client = new Client({
44-
name: "test client",
45-
version: "1.0",
46-
});
43+
const client = new Client(
44+
{
45+
name: "test client",
46+
version: "1.0",
47+
},
48+
{
49+
sampling: {},
50+
},
51+
);
4752

4853
await client.connect(clientTransport);
4954

@@ -82,10 +87,15 @@ test("should initialize with supported older protocol version", async () => {
8287
}),
8388
};
8489

85-
const client = new Client({
86-
name: "test client",
87-
version: "1.0",
88-
});
90+
const client = new Client(
91+
{
92+
name: "test client",
93+
version: "1.0",
94+
},
95+
{
96+
sampling: {},
97+
},
98+
);
8999

90100
await client.connect(clientTransport);
91101

@@ -119,10 +129,15 @@ test("should reject unsupported protocol version", async () => {
119129
}),
120130
};
121131

122-
const client = new Client({
123-
name: "test client",
124-
version: "1.0",
125-
});
132+
const client = new Client(
133+
{
134+
name: "test client",
135+
version: "1.0",
136+
},
137+
{
138+
sampling: {},
139+
},
140+
);
126141

127142
await expect(client.connect(clientTransport)).rejects.toThrow(
128143
"Server's protocol version is not supported: invalid-version",
@@ -132,12 +147,18 @@ test("should reject unsupported protocol version", async () => {
132147
});
133148

134149
test("should respect server capabilities", async () => {
135-
const server = new Server({
136-
name: "test server",
137-
version: "1.0",
138-
});
150+
const server = new Server(
151+
{
152+
name: "test server",
153+
version: "1.0",
154+
},
155+
{
156+
resources: {},
157+
tools: {},
158+
},
159+
);
139160

140-
server.setRequestHandler(InitializeRequestSchema, (request) => ({
161+
server.setRequestHandler(InitializeRequestSchema, (_request) => ({
141162
protocolVersion: LATEST_PROTOCOL_VERSION,
142163
capabilities: {
143164
resources: {},
@@ -160,10 +181,15 @@ test("should respect server capabilities", async () => {
160181
const [clientTransport, serverTransport] =
161182
InMemoryTransport.createLinkedPair();
162183

163-
const client = new Client({
164-
name: "test client",
165-
version: "1.0",
166-
});
184+
const client = new Client(
185+
{
186+
name: "test client",
187+
version: "1.0",
188+
},
189+
{
190+
sampling: {},
191+
},
192+
);
167193

168194
await Promise.all([
169195
client.connect(clientTransport),
@@ -231,10 +257,15 @@ test("should typecheck", () => {
231257
WeatherRequest,
232258
WeatherNotification,
233259
WeatherResult
234-
>({
235-
name: "WeatherClient",
236-
version: "1.0.0",
237-
});
260+
>(
261+
{
262+
name: "WeatherClient",
263+
version: "1.0.0",
264+
},
265+
{
266+
sampling: {},
267+
},
268+
);
238269

239270
// Typecheck that only valid weather requests/notifications/results are allowed
240271
false &&

src/client/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Transport } from "../shared/transport.js";
33
import {
44
CallToolRequest,
55
CallToolResultSchema,
6+
ClientCapabilities,
67
ClientNotification,
78
ClientRequest,
89
ClientResult,
@@ -75,7 +76,10 @@ export class Client<
7576
/**
7677
* Initializes this client with the given name and version information.
7778
*/
78-
constructor(private _clientInfo: Implementation) {
79+
constructor(
80+
private _clientInfo: Implementation,
81+
private _capabilities: ClientCapabilities,
82+
) {
7983
super();
8084
}
8185

@@ -88,7 +92,7 @@ export class Client<
8892
method: "initialize",
8993
params: {
9094
protocolVersion: LATEST_PROTOCOL_VERSION,
91-
capabilities: {},
95+
capabilities: this._capabilities,
9296
clientInfo: this._clientInfo,
9397
},
9498
},

src/server/index.test.ts

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ResultSchema,
1010
LATEST_PROTOCOL_VERSION,
1111
SUPPORTED_PROTOCOL_VERSIONS,
12+
CreateMessageRequestSchema,
1213
} from "../types.js";
1314
import { Transport } from "../shared/transport.js";
1415
import { InMemoryTransport } from "../inMemory.js";
@@ -39,10 +40,18 @@ test("should accept latest protocol version", async () => {
3940
}),
4041
};
4142

42-
const server = new Server({
43-
name: "test server",
44-
version: "1.0",
45-
});
43+
const server = new Server(
44+
{
45+
name: "test server",
46+
version: "1.0",
47+
},
48+
{
49+
prompts: {},
50+
resources: {},
51+
tools: {},
52+
logging: {},
53+
},
54+
);
4655

4756
await server.connect(serverTransport);
4857

@@ -90,10 +99,18 @@ test("should accept supported older protocol version", async () => {
9099
}),
91100
};
92101

93-
const server = new Server({
94-
name: "test server",
95-
version: "1.0",
96-
});
102+
const server = new Server(
103+
{
104+
name: "test server",
105+
version: "1.0",
106+
},
107+
{
108+
prompts: {},
109+
resources: {},
110+
tools: {},
111+
logging: {},
112+
},
113+
);
97114

98115
await server.connect(serverTransport);
99116

@@ -140,10 +157,18 @@ test("should handle unsupported protocol version", async () => {
140157
}),
141158
};
142159

143-
const server = new Server({
144-
name: "test server",
145-
version: "1.0",
146-
});
160+
const server = new Server(
161+
{
162+
name: "test server",
163+
version: "1.0",
164+
},
165+
{
166+
prompts: {},
167+
resources: {},
168+
tools: {},
169+
logging: {},
170+
},
171+
);
147172

148173
await server.connect(serverTransport);
149174

@@ -166,14 +191,39 @@ test("should handle unsupported protocol version", async () => {
166191
});
167192

168193
test("should respect client capabilities", async () => {
169-
const server = new Server({
170-
name: "test server",
171-
version: "1.0",
172-
});
194+
const server = new Server(
195+
{
196+
name: "test server",
197+
version: "1.0",
198+
},
199+
{
200+
prompts: {},
201+
resources: {},
202+
tools: {},
203+
logging: {},
204+
},
205+
);
206+
const client = new Client(
207+
{
208+
name: "test client",
209+
version: "1.0",
210+
},
211+
{
212+
sampling: {},
213+
},
214+
);
173215

174-
const client = new Client({
175-
name: "test client",
176-
version: "1.0",
216+
// Implement request handler for sampling/createMessage
217+
client.setRequestHandler(CreateMessageRequestSchema, async (request) => {
218+
// Mock implementation of createMessage
219+
return {
220+
model: "test-model",
221+
role: "assistant",
222+
content: {
223+
type: "text",
224+
text: "This is a test response",
225+
},
226+
};
177227
});
178228

179229
const [clientTransport, serverTransport] =
@@ -184,9 +234,17 @@ test("should respect client capabilities", async () => {
184234
server.connect(serverTransport),
185235
]);
186236

187-
expect(server.getClientCapabilities()).toEqual({});
237+
expect(server.getClientCapabilities()).toEqual({ sampling: {} });
238+
239+
// This should work because sampling is supported by the client
240+
await expect(
241+
server.createMessage({
242+
messages: [],
243+
maxTokens: 10,
244+
}),
245+
).resolves.not.toThrow();
188246

189-
// This should throw because roots are not supported by the client
247+
// This should still throw because roots are not supported by the client
190248
await expect(server.listRoots()).rejects.toThrow(
191249
"Client does not support roots",
192250
);
@@ -237,10 +295,18 @@ test("should typecheck", () => {
237295
WeatherRequest,
238296
WeatherNotification,
239297
WeatherResult
240-
>({
241-
name: "WeatherServer",
242-
version: "1.0.0",
243-
});
298+
>(
299+
{
300+
name: "WeatherServer",
301+
version: "1.0.0",
302+
},
303+
{
304+
prompts: {},
305+
resources: {},
306+
tools: {},
307+
logging: {},
308+
},
309+
);
244310

245311
// Typecheck that only valid weather requests/notifications/results are allowed
246312
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {

0 commit comments

Comments
 (0)