Check capabilities when request handlers are set
This commit is contained in:
parent
64c2862883
commit
8b307a5d62
1 changed files with 155 additions and 1 deletions
|
|
@ -10,6 +10,10 @@ import {
|
|||
LATEST_PROTOCOL_VERSION,
|
||||
SUPPORTED_PROTOCOL_VERSIONS,
|
||||
CreateMessageRequestSchema,
|
||||
ListPromptsRequestSchema,
|
||||
ListResourcesRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
SetLevelRequestSchema,
|
||||
} from "../types.js";
|
||||
import { Transport } from "../shared/transport.js";
|
||||
import { InMemoryTransport } from "../inMemory.js";
|
||||
|
|
@ -293,6 +297,41 @@ test("should respect server notification capabilities", async () => {
|
|||
).rejects.toThrow(/^Server does not support/);
|
||||
});
|
||||
|
||||
test("should only allow setRequestHandler for declared capabilities", () => {
|
||||
const server = new Server(
|
||||
{
|
||||
name: "test server",
|
||||
version: "1.0",
|
||||
},
|
||||
{
|
||||
capabilities: {
|
||||
prompts: {},
|
||||
resources: {},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// These should work because the capabilities are declared
|
||||
expect(() => {
|
||||
server.setRequestHandler(ListPromptsRequestSchema, () => ({ prompts: [] }));
|
||||
}).not.toThrow();
|
||||
|
||||
expect(() => {
|
||||
server.setRequestHandler(ListResourcesRequestSchema, () => ({
|
||||
resources: [],
|
||||
}));
|
||||
}).not.toThrow();
|
||||
|
||||
// These should throw because the capabilities are not declared
|
||||
expect(() => {
|
||||
server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: [] }));
|
||||
}).toThrow(/^Server does not support tools/);
|
||||
|
||||
expect(() => {
|
||||
server.setRequestHandler(SetLevelRequestSchema, () => ({}));
|
||||
}).toThrow(/^Server does not support logging/);
|
||||
});
|
||||
|
||||
/*
|
||||
Test that custom request/notification/result schemas can be used with the Server class.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue