@contextvm/mcp-sdk 1.27.1-contextvm.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/LICENSE +21 -0
- package/README.md +141 -0
- package/dist/cjs/client/index.d.ts +588 -0
- package/dist/cjs/client/index.d.ts.map +1 -0
- package/dist/cjs/client/index.js +629 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/client/stdio.d.ts +77 -0
- package/dist/cjs/client/stdio.d.ts.map +1 -0
- package/dist/cjs/client/stdio.js +199 -0
- package/dist/cjs/client/stdio.js.map +1 -0
- package/dist/cjs/experimental/index.d.ts +13 -0
- package/dist/cjs/experimental/index.d.ts.map +1 -0
- package/dist/cjs/experimental/index.js +29 -0
- package/dist/cjs/experimental/index.js.map +1 -0
- package/dist/cjs/experimental/tasks/client.d.ts +121 -0
- package/dist/cjs/experimental/tasks/client.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/client.js +188 -0
- package/dist/cjs/experimental/tasks/client.js.map +1 -0
- package/dist/cjs/experimental/tasks/helpers.d.ts +47 -0
- package/dist/cjs/experimental/tasks/helpers.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/helpers.js +68 -0
- package/dist/cjs/experimental/tasks/helpers.js.map +1 -0
- package/dist/cjs/experimental/tasks/index.d.ts +16 -0
- package/dist/cjs/experimental/tasks/index.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/index.js +39 -0
- package/dist/cjs/experimental/tasks/index.js.map +1 -0
- package/dist/cjs/experimental/tasks/interfaces.d.ts +232 -0
- package/dist/cjs/experimental/tasks/interfaces.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/interfaces.js +19 -0
- package/dist/cjs/experimental/tasks/interfaces.js.map +1 -0
- package/dist/cjs/experimental/tasks/mcp-server.d.ts +77 -0
- package/dist/cjs/experimental/tasks/mcp-server.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/mcp-server.js +36 -0
- package/dist/cjs/experimental/tasks/mcp-server.js.map +1 -0
- package/dist/cjs/experimental/tasks/server.d.ts +170 -0
- package/dist/cjs/experimental/tasks/server.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/server.js +250 -0
- package/dist/cjs/experimental/tasks/server.js.map +1 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts +94 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.js +251 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.js.map +1 -0
- package/dist/cjs/experimental/tasks/types.d.ts +10 -0
- package/dist/cjs/experimental/tasks/types.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/types.js +28 -0
- package/dist/cjs/experimental/tasks/types.js.map +1 -0
- package/dist/cjs/inMemory.d.ts +31 -0
- package/dist/cjs/inMemory.d.ts.map +1 -0
- package/dist/cjs/inMemory.js +51 -0
- package/dist/cjs/inMemory.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/server/completable.d.ts +38 -0
- package/dist/cjs/server/completable.d.ts.map +1 -0
- package/dist/cjs/server/completable.js +48 -0
- package/dist/cjs/server/completable.js.map +1 -0
- package/dist/cjs/server/index.d.ts +196 -0
- package/dist/cjs/server/index.d.ts.map +1 -0
- package/dist/cjs/server/index.js +444 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/server/mcp.d.ts +364 -0
- package/dist/cjs/server/mcp.d.ts.map +1 -0
- package/dist/cjs/server/mcp.js +918 -0
- package/dist/cjs/server/mcp.js.map +1 -0
- package/dist/cjs/server/stdio.d.ts +28 -0
- package/dist/cjs/server/stdio.d.ts.map +1 -0
- package/dist/cjs/server/stdio.js +82 -0
- package/dist/cjs/server/stdio.js.map +1 -0
- package/dist/cjs/server/zod-compat.d.ts +84 -0
- package/dist/cjs/server/zod-compat.d.ts.map +1 -0
- package/dist/cjs/server/zod-compat.js +244 -0
- package/dist/cjs/server/zod-compat.js.map +1 -0
- package/dist/cjs/server/zod-json-schema-compat.d.ts +12 -0
- package/dist/cjs/server/zod-json-schema-compat.d.ts.map +1 -0
- package/dist/cjs/server/zod-json-schema-compat.js +79 -0
- package/dist/cjs/server/zod-json-schema-compat.js.map +1 -0
- package/dist/cjs/shared/auth-info.d.ts +32 -0
- package/dist/cjs/shared/auth-info.d.ts.map +1 -0
- package/dist/cjs/shared/auth-info.js +3 -0
- package/dist/cjs/shared/auth-info.js.map +1 -0
- package/dist/cjs/shared/metadataUtils.d.ts +16 -0
- package/dist/cjs/shared/metadataUtils.d.ts.map +1 -0
- package/dist/cjs/shared/metadataUtils.js +25 -0
- package/dist/cjs/shared/metadataUtils.js.map +1 -0
- package/dist/cjs/shared/protocol.d.ts +443 -0
- package/dist/cjs/shared/protocol.d.ts.map +1 -0
- package/dist/cjs/shared/protocol.js +1104 -0
- package/dist/cjs/shared/protocol.js.map +1 -0
- package/dist/cjs/shared/responseMessage.d.ts +45 -0
- package/dist/cjs/shared/responseMessage.d.ts.map +1 -0
- package/dist/cjs/shared/responseMessage.js +23 -0
- package/dist/cjs/shared/responseMessage.js.map +1 -0
- package/dist/cjs/shared/stdio.d.ts +13 -0
- package/dist/cjs/shared/stdio.d.ts.map +1 -0
- package/dist/cjs/shared/stdio.js +37 -0
- package/dist/cjs/shared/stdio.js.map +1 -0
- package/dist/cjs/shared/toolNameValidation.d.ts +31 -0
- package/dist/cjs/shared/toolNameValidation.d.ts.map +1 -0
- package/dist/cjs/shared/toolNameValidation.js +97 -0
- package/dist/cjs/shared/toolNameValidation.js.map +1 -0
- package/dist/cjs/shared/transport.d.ts +89 -0
- package/dist/cjs/shared/transport.d.ts.map +1 -0
- package/dist/cjs/shared/transport.js +43 -0
- package/dist/cjs/shared/transport.js.map +1 -0
- package/dist/cjs/shared/uriTemplate.d.ts +25 -0
- package/dist/cjs/shared/uriTemplate.d.ts.map +1 -0
- package/dist/cjs/shared/uriTemplate.js +243 -0
- package/dist/cjs/shared/uriTemplate.js.map +1 -0
- package/dist/cjs/spec.types.d.ts +2299 -0
- package/dist/cjs/spec.types.d.ts.map +1 -0
- package/dist/cjs/spec.types.js +27 -0
- package/dist/cjs/spec.types.js.map +1 -0
- package/dist/cjs/types.d.ts +8137 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +2092 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validation/ajv-provider.d.ts +53 -0
- package/dist/cjs/validation/ajv-provider.d.ts.map +1 -0
- package/dist/cjs/validation/ajv-provider.js +94 -0
- package/dist/cjs/validation/ajv-provider.js.map +1 -0
- package/dist/cjs/validation/cfworker-provider.d.ts +51 -0
- package/dist/cjs/validation/cfworker-provider.d.ts.map +1 -0
- package/dist/cjs/validation/cfworker-provider.js +69 -0
- package/dist/cjs/validation/cfworker-provider.js.map +1 -0
- package/dist/cjs/validation/index.d.ts +29 -0
- package/dist/cjs/validation/index.d.ts.map +1 -0
- package/dist/cjs/validation/index.js +30 -0
- package/dist/cjs/validation/index.js.map +1 -0
- package/dist/cjs/validation/types.d.ts +65 -0
- package/dist/cjs/validation/types.d.ts.map +1 -0
- package/dist/cjs/validation/types.js +3 -0
- package/dist/cjs/validation/types.js.map +1 -0
- package/dist/esm/client/index.d.ts +588 -0
- package/dist/esm/client/index.d.ts.map +1 -0
- package/dist/esm/client/index.js +624 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/client/stdio.d.ts +77 -0
- package/dist/esm/client/stdio.d.ts.map +1 -0
- package/dist/esm/client/stdio.js +191 -0
- package/dist/esm/client/stdio.js.map +1 -0
- package/dist/esm/experimental/index.d.ts +13 -0
- package/dist/esm/experimental/index.d.ts.map +1 -0
- package/dist/esm/experimental/index.js +13 -0
- package/dist/esm/experimental/index.js.map +1 -0
- package/dist/esm/experimental/tasks/client.d.ts +121 -0
- package/dist/esm/experimental/tasks/client.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/client.js +184 -0
- package/dist/esm/experimental/tasks/client.js.map +1 -0
- package/dist/esm/experimental/tasks/helpers.d.ts +47 -0
- package/dist/esm/experimental/tasks/helpers.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/helpers.js +64 -0
- package/dist/esm/experimental/tasks/helpers.js.map +1 -0
- package/dist/esm/experimental/tasks/index.d.ts +16 -0
- package/dist/esm/experimental/tasks/index.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/index.js +20 -0
- package/dist/esm/experimental/tasks/index.js.map +1 -0
- package/dist/esm/experimental/tasks/interfaces.d.ts +232 -0
- package/dist/esm/experimental/tasks/interfaces.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/interfaces.js +16 -0
- package/dist/esm/experimental/tasks/interfaces.js.map +1 -0
- package/dist/esm/experimental/tasks/mcp-server.d.ts +77 -0
- package/dist/esm/experimental/tasks/mcp-server.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/mcp-server.js +32 -0
- package/dist/esm/experimental/tasks/mcp-server.js.map +1 -0
- package/dist/esm/experimental/tasks/server.d.ts +170 -0
- package/dist/esm/experimental/tasks/server.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/server.js +246 -0
- package/dist/esm/experimental/tasks/server.js.map +1 -0
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts +94 -0
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/stores/in-memory.js +246 -0
- package/dist/esm/experimental/tasks/stores/in-memory.js.map +1 -0
- package/dist/esm/experimental/tasks/types.d.ts +10 -0
- package/dist/esm/experimental/tasks/types.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/types.js +10 -0
- package/dist/esm/experimental/tasks/types.js.map +1 -0
- package/dist/esm/inMemory.d.ts +31 -0
- package/dist/esm/inMemory.d.ts.map +1 -0
- package/dist/esm/inMemory.js +47 -0
- package/dist/esm/inMemory.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/server/completable.d.ts +38 -0
- package/dist/esm/server/completable.d.ts.map +1 -0
- package/dist/esm/server/completable.js +41 -0
- package/dist/esm/server/completable.js.map +1 -0
- package/dist/esm/server/index.d.ts +196 -0
- package/dist/esm/server/index.d.ts.map +1 -0
- package/dist/esm/server/index.js +440 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/server/mcp.d.ts +364 -0
- package/dist/esm/server/mcp.d.ts.map +1 -0
- package/dist/esm/server/mcp.js +913 -0
- package/dist/esm/server/mcp.js.map +1 -0
- package/dist/esm/server/stdio.d.ts +28 -0
- package/dist/esm/server/stdio.d.ts.map +1 -0
- package/dist/esm/server/stdio.js +75 -0
- package/dist/esm/server/stdio.js.map +1 -0
- package/dist/esm/server/zod-compat.d.ts +84 -0
- package/dist/esm/server/zod-compat.d.ts.map +1 -0
- package/dist/esm/server/zod-compat.js +209 -0
- package/dist/esm/server/zod-compat.js.map +1 -0
- package/dist/esm/server/zod-json-schema-compat.d.ts +12 -0
- package/dist/esm/server/zod-json-schema-compat.d.ts.map +1 -0
- package/dist/esm/server/zod-json-schema-compat.js +51 -0
- package/dist/esm/server/zod-json-schema-compat.js.map +1 -0
- package/dist/esm/shared/auth-info.d.ts +32 -0
- package/dist/esm/shared/auth-info.d.ts.map +1 -0
- package/dist/esm/shared/auth-info.js +2 -0
- package/dist/esm/shared/auth-info.js.map +1 -0
- package/dist/esm/shared/metadataUtils.d.ts +16 -0
- package/dist/esm/shared/metadataUtils.d.ts.map +1 -0
- package/dist/esm/shared/metadataUtils.js +22 -0
- package/dist/esm/shared/metadataUtils.js.map +1 -0
- package/dist/esm/shared/protocol.d.ts +443 -0
- package/dist/esm/shared/protocol.d.ts.map +1 -0
- package/dist/esm/shared/protocol.js +1099 -0
- package/dist/esm/shared/protocol.js.map +1 -0
- package/dist/esm/shared/responseMessage.d.ts +45 -0
- package/dist/esm/shared/responseMessage.d.ts.map +1 -0
- package/dist/esm/shared/responseMessage.js +19 -0
- package/dist/esm/shared/responseMessage.js.map +1 -0
- package/dist/esm/shared/stdio.d.ts +13 -0
- package/dist/esm/shared/stdio.d.ts.map +1 -0
- package/dist/esm/shared/stdio.js +31 -0
- package/dist/esm/shared/stdio.js.map +1 -0
- package/dist/esm/shared/toolNameValidation.d.ts +31 -0
- package/dist/esm/shared/toolNameValidation.d.ts.map +1 -0
- package/dist/esm/shared/toolNameValidation.js +92 -0
- package/dist/esm/shared/toolNameValidation.js.map +1 -0
- package/dist/esm/shared/transport.d.ts +89 -0
- package/dist/esm/shared/transport.d.ts.map +1 -0
- package/dist/esm/shared/transport.js +39 -0
- package/dist/esm/shared/transport.js.map +1 -0
- package/dist/esm/shared/uriTemplate.d.ts +25 -0
- package/dist/esm/shared/uriTemplate.d.ts.map +1 -0
- package/dist/esm/shared/uriTemplate.js +239 -0
- package/dist/esm/shared/uriTemplate.js.map +1 -0
- package/dist/esm/spec.types.d.ts +2299 -0
- package/dist/esm/spec.types.d.ts.map +1 -0
- package/dist/esm/spec.types.js +24 -0
- package/dist/esm/spec.types.js.map +1 -0
- package/dist/esm/types.d.ts +8137 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2052 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validation/ajv-provider.d.ts +53 -0
- package/dist/esm/validation/ajv-provider.d.ts.map +1 -0
- package/dist/esm/validation/ajv-provider.js +87 -0
- package/dist/esm/validation/ajv-provider.js.map +1 -0
- package/dist/esm/validation/cfworker-provider.d.ts +51 -0
- package/dist/esm/validation/cfworker-provider.d.ts.map +1 -0
- package/dist/esm/validation/cfworker-provider.js +65 -0
- package/dist/esm/validation/cfworker-provider.js.map +1 -0
- package/dist/esm/validation/index.d.ts +29 -0
- package/dist/esm/validation/index.d.ts.map +1 -0
- package/dist/esm/validation/index.js +29 -0
- package/dist/esm/validation/index.js.map +1 -0
- package/dist/esm/validation/types.d.ts +65 -0
- package/dist/esm/validation/types.d.ts.map +1 -0
- package/dist/esm/validation/types.js +2 -0
- package/dist/esm/validation/types.js.map +1 -0
- package/package.json +124 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { Protocol, type NotificationOptions, type ProtocolOptions, type RequestOptions } from '../shared/protocol.js';
|
|
2
|
+
import { type ClientCapabilities, type CreateMessageRequest, type CreateMessageResult, type CreateMessageResultWithTools, type CreateMessageRequestParamsBase, type CreateMessageRequestParamsWithTools, type ElicitRequestFormParams, type ElicitRequestURLParams, type ElicitResult, type Implementation, type ListRootsRequest, type LoggingMessageNotification, type ResourceUpdatedNotification, type ServerCapabilities, type ServerNotification, type ServerRequest, type ServerResult, type Request, type Notification, type Result } from '../types.js';
|
|
3
|
+
import type { jsonSchemaValidator } from '../validation/types.js';
|
|
4
|
+
import { AnyObjectSchema, SchemaOutput } from './zod-compat.js';
|
|
5
|
+
import { RequestHandlerExtra } from '../shared/protocol.js';
|
|
6
|
+
import { ExperimentalServerTasks } from '../experimental/tasks/server.js';
|
|
7
|
+
export type ServerOptions = ProtocolOptions & {
|
|
8
|
+
/**
|
|
9
|
+
* Capabilities to advertise as being supported by this server.
|
|
10
|
+
*/
|
|
11
|
+
capabilities?: ServerCapabilities;
|
|
12
|
+
/**
|
|
13
|
+
* Optional instructions describing how to use the server and its features.
|
|
14
|
+
*/
|
|
15
|
+
instructions?: string;
|
|
16
|
+
/**
|
|
17
|
+
* JSON Schema validator for elicitation response validation.
|
|
18
|
+
*
|
|
19
|
+
* The validator is used to validate user input returned from elicitation
|
|
20
|
+
* requests against the requested schema.
|
|
21
|
+
*
|
|
22
|
+
* @default AjvJsonSchemaValidator
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // ajv (default)
|
|
27
|
+
* const server = new Server(
|
|
28
|
+
* { name: 'my-server', version: '1.0.0' },
|
|
29
|
+
* {
|
|
30
|
+
* capabilities: {}
|
|
31
|
+
* jsonSchemaValidator: new AjvJsonSchemaValidator()
|
|
32
|
+
* }
|
|
33
|
+
* );
|
|
34
|
+
*
|
|
35
|
+
* // @cfworker/json-schema
|
|
36
|
+
* const server = new Server(
|
|
37
|
+
* { name: 'my-server', version: '1.0.0' },
|
|
38
|
+
* {
|
|
39
|
+
* capabilities: {},
|
|
40
|
+
* jsonSchemaValidator: new CfWorkerJsonSchemaValidator()
|
|
41
|
+
* }
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
jsonSchemaValidator?: jsonSchemaValidator;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* An MCP server on top of a pluggable transport.
|
|
49
|
+
*
|
|
50
|
+
* This server will automatically respond to the initialization flow as initiated from the client.
|
|
51
|
+
*
|
|
52
|
+
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
|
|
53
|
+
*
|
|
54
|
+
* ```typescript
|
|
55
|
+
* // Custom schemas
|
|
56
|
+
* const CustomRequestSchema = RequestSchema.extend({...})
|
|
57
|
+
* const CustomNotificationSchema = NotificationSchema.extend({...})
|
|
58
|
+
* const CustomResultSchema = ResultSchema.extend({...})
|
|
59
|
+
*
|
|
60
|
+
* // Type aliases
|
|
61
|
+
* type CustomRequest = z.infer<typeof CustomRequestSchema>
|
|
62
|
+
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
|
|
63
|
+
* type CustomResult = z.infer<typeof CustomResultSchema>
|
|
64
|
+
*
|
|
65
|
+
* // Create typed server
|
|
66
|
+
* const server = new Server<CustomRequest, CustomNotification, CustomResult>({
|
|
67
|
+
* name: "CustomServer",
|
|
68
|
+
* version: "1.0.0"
|
|
69
|
+
* })
|
|
70
|
+
* ```
|
|
71
|
+
* @deprecated Use `McpServer` instead for the high-level API. Only use `Server` for advanced use cases.
|
|
72
|
+
*/
|
|
73
|
+
export declare class Server<RequestT extends Request = Request, NotificationT extends Notification = Notification, ResultT extends Result = Result> extends Protocol<ServerRequest | RequestT, ServerNotification | NotificationT, ServerResult | ResultT> {
|
|
74
|
+
private _serverInfo;
|
|
75
|
+
private _clientCapabilities?;
|
|
76
|
+
private _clientVersion?;
|
|
77
|
+
private _capabilities;
|
|
78
|
+
private _instructions?;
|
|
79
|
+
private _jsonSchemaValidator;
|
|
80
|
+
private _experimental?;
|
|
81
|
+
/**
|
|
82
|
+
* Callback for when initialization has fully completed (i.e., the client has sent an `initialized` notification).
|
|
83
|
+
*/
|
|
84
|
+
oninitialized?: () => void;
|
|
85
|
+
/**
|
|
86
|
+
* Initializes this server with the given name and version information.
|
|
87
|
+
*/
|
|
88
|
+
constructor(_serverInfo: Implementation, options?: ServerOptions);
|
|
89
|
+
/**
|
|
90
|
+
* Access experimental features.
|
|
91
|
+
*
|
|
92
|
+
* WARNING: These APIs are experimental and may change without notice.
|
|
93
|
+
*
|
|
94
|
+
* @experimental
|
|
95
|
+
*/
|
|
96
|
+
get experimental(): {
|
|
97
|
+
tasks: ExperimentalServerTasks<RequestT, NotificationT, ResultT>;
|
|
98
|
+
};
|
|
99
|
+
private _loggingLevels;
|
|
100
|
+
private readonly LOG_LEVEL_SEVERITY;
|
|
101
|
+
private isMessageIgnored;
|
|
102
|
+
/**
|
|
103
|
+
* Registers new capabilities. This can only be called before connecting to a transport.
|
|
104
|
+
*
|
|
105
|
+
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
|
|
106
|
+
*/
|
|
107
|
+
registerCapabilities(capabilities: ServerCapabilities): void;
|
|
108
|
+
/**
|
|
109
|
+
* Override request handler registration to enforce server-side validation for tools/call.
|
|
110
|
+
*/
|
|
111
|
+
setRequestHandler<T extends AnyObjectSchema>(requestSchema: T, handler: (request: SchemaOutput<T>, extra: RequestHandlerExtra<ServerRequest | RequestT, ServerNotification | NotificationT>) => ServerResult | ResultT | Promise<ServerResult | ResultT>): void;
|
|
112
|
+
protected assertCapabilityForMethod(method: RequestT['method']): void;
|
|
113
|
+
protected assertNotificationCapability(method: (ServerNotification | NotificationT)['method']): void;
|
|
114
|
+
protected assertRequestHandlerCapability(method: string): void;
|
|
115
|
+
protected assertTaskCapability(method: string): void;
|
|
116
|
+
protected assertTaskHandlerCapability(method: string): void;
|
|
117
|
+
private _oninitialize;
|
|
118
|
+
/**
|
|
119
|
+
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
120
|
+
*/
|
|
121
|
+
getClientCapabilities(): ClientCapabilities | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
124
|
+
*/
|
|
125
|
+
getClientVersion(): Implementation | undefined;
|
|
126
|
+
private getCapabilities;
|
|
127
|
+
ping(): Promise<{
|
|
128
|
+
_meta?: {
|
|
129
|
+
[x: string]: unknown;
|
|
130
|
+
progressToken?: string | number | undefined;
|
|
131
|
+
"io.modelcontextprotocol/related-task"?: {
|
|
132
|
+
taskId: string;
|
|
133
|
+
} | undefined;
|
|
134
|
+
} | undefined;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* Request LLM sampling from the client (without tools).
|
|
138
|
+
* Returns single content block for backwards compatibility.
|
|
139
|
+
*/
|
|
140
|
+
createMessage(params: CreateMessageRequestParamsBase, options?: RequestOptions): Promise<CreateMessageResult>;
|
|
141
|
+
/**
|
|
142
|
+
* Request LLM sampling from the client with tool support.
|
|
143
|
+
* Returns content that may be a single block or array (for parallel tool calls).
|
|
144
|
+
*/
|
|
145
|
+
createMessage(params: CreateMessageRequestParamsWithTools, options?: RequestOptions): Promise<CreateMessageResultWithTools>;
|
|
146
|
+
/**
|
|
147
|
+
* Request LLM sampling from the client.
|
|
148
|
+
* When tools may or may not be present, returns the union type.
|
|
149
|
+
*/
|
|
150
|
+
createMessage(params: CreateMessageRequest['params'], options?: RequestOptions): Promise<CreateMessageResult | CreateMessageResultWithTools>;
|
|
151
|
+
/**
|
|
152
|
+
* Creates an elicitation request for the given parameters.
|
|
153
|
+
* For backwards compatibility, `mode` may be omitted for form requests and will default to `'form'`.
|
|
154
|
+
* @param params The parameters for the elicitation request.
|
|
155
|
+
* @param options Optional request options.
|
|
156
|
+
* @returns The result of the elicitation request.
|
|
157
|
+
*/
|
|
158
|
+
elicitInput(params: ElicitRequestFormParams | ElicitRequestURLParams, options?: RequestOptions): Promise<ElicitResult>;
|
|
159
|
+
/**
|
|
160
|
+
* Creates a reusable callback that, when invoked, will send a `notifications/elicitation/complete`
|
|
161
|
+
* notification for the specified elicitation ID.
|
|
162
|
+
*
|
|
163
|
+
* @param elicitationId The ID of the elicitation to mark as complete.
|
|
164
|
+
* @param options Optional notification options. Useful when the completion notification should be related to a prior request.
|
|
165
|
+
* @returns A function that emits the completion notification when awaited.
|
|
166
|
+
*/
|
|
167
|
+
createElicitationCompletionNotifier(elicitationId: string, options?: NotificationOptions): () => Promise<void>;
|
|
168
|
+
listRoots(params?: ListRootsRequest['params'], options?: RequestOptions): Promise<{
|
|
169
|
+
[x: string]: unknown;
|
|
170
|
+
roots: {
|
|
171
|
+
uri: string;
|
|
172
|
+
name?: string | undefined;
|
|
173
|
+
_meta?: Record<string, unknown> | undefined;
|
|
174
|
+
}[];
|
|
175
|
+
_meta?: {
|
|
176
|
+
[x: string]: unknown;
|
|
177
|
+
progressToken?: string | number | undefined;
|
|
178
|
+
"io.modelcontextprotocol/related-task"?: {
|
|
179
|
+
taskId: string;
|
|
180
|
+
} | undefined;
|
|
181
|
+
} | undefined;
|
|
182
|
+
}>;
|
|
183
|
+
/**
|
|
184
|
+
* Sends a logging message to the client, if connected.
|
|
185
|
+
* Note: You only need to send the parameters object, not the entire JSON RPC message
|
|
186
|
+
* @see LoggingMessageNotification
|
|
187
|
+
* @param params
|
|
188
|
+
* @param sessionId optional for stateless and backward compatibility
|
|
189
|
+
*/
|
|
190
|
+
sendLoggingMessage(params: LoggingMessageNotification['params'], sessionId?: string): Promise<void>;
|
|
191
|
+
sendResourceUpdated(params: ResourceUpdatedNotification['params']): Promise<void>;
|
|
192
|
+
sendResourceListChanged(): Promise<void>;
|
|
193
|
+
sendToolListChanged(): Promise<void>;
|
|
194
|
+
sendPromptListChanged(): Promise<void>;
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@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BAAkB,CAAC,MAAM,EAAE,0BAA0B,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,EAAE,MAAM;IAQnF,mBAAmB,CAAC,MAAM,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAOjE,uBAAuB;IAMvB,mBAAmB;IAInB,qBAAqB;CAG9B"}
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { mergeCapabilities, Protocol } from '../shared/protocol.js';
|
|
2
|
+
import { CreateMessageResultSchema, CreateMessageResultWithToolsSchema, ElicitResultSchema, EmptyResultSchema, ErrorCode, InitializedNotificationSchema, InitializeRequestSchema, LATEST_PROTOCOL_VERSION, ListRootsResultSchema, LoggingLevelSchema, McpError, SetLevelRequestSchema, SUPPORTED_PROTOCOL_VERSIONS, CallToolRequestSchema, CallToolResultSchema, CreateTaskResultSchema } from '../types.js';
|
|
3
|
+
import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js';
|
|
4
|
+
import { getObjectShape, isZ4Schema, safeParse } from './zod-compat.js';
|
|
5
|
+
import { ExperimentalServerTasks } from '../experimental/tasks/server.js';
|
|
6
|
+
import { assertToolsCallTaskCapability, assertClientRequestTaskCapability } from '../experimental/tasks/helpers.js';
|
|
7
|
+
/**
|
|
8
|
+
* An MCP server on top of a pluggable transport.
|
|
9
|
+
*
|
|
10
|
+
* This server will automatically respond to the initialization flow as initiated from the client.
|
|
11
|
+
*
|
|
12
|
+
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Custom schemas
|
|
16
|
+
* const CustomRequestSchema = RequestSchema.extend({...})
|
|
17
|
+
* const CustomNotificationSchema = NotificationSchema.extend({...})
|
|
18
|
+
* const CustomResultSchema = ResultSchema.extend({...})
|
|
19
|
+
*
|
|
20
|
+
* // Type aliases
|
|
21
|
+
* type CustomRequest = z.infer<typeof CustomRequestSchema>
|
|
22
|
+
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
|
|
23
|
+
* type CustomResult = z.infer<typeof CustomResultSchema>
|
|
24
|
+
*
|
|
25
|
+
* // Create typed server
|
|
26
|
+
* const server = new Server<CustomRequest, CustomNotification, CustomResult>({
|
|
27
|
+
* name: "CustomServer",
|
|
28
|
+
* version: "1.0.0"
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
* @deprecated Use `McpServer` instead for the high-level API. Only use `Server` for advanced use cases.
|
|
32
|
+
*/
|
|
33
|
+
export class Server extends Protocol {
|
|
34
|
+
/**
|
|
35
|
+
* Initializes this server with the given name and version information.
|
|
36
|
+
*/
|
|
37
|
+
constructor(_serverInfo, options) {
|
|
38
|
+
super(options);
|
|
39
|
+
this._serverInfo = _serverInfo;
|
|
40
|
+
// Map log levels by session id
|
|
41
|
+
this._loggingLevels = new Map();
|
|
42
|
+
// Map LogLevelSchema to severity index
|
|
43
|
+
this.LOG_LEVEL_SEVERITY = new Map(LoggingLevelSchema.options.map((level, index) => [level, index]));
|
|
44
|
+
// Is a message with the given level ignored in the log level set for the given session id?
|
|
45
|
+
this.isMessageIgnored = (level, sessionId) => {
|
|
46
|
+
const currentLevel = this._loggingLevels.get(sessionId);
|
|
47
|
+
return currentLevel ? this.LOG_LEVEL_SEVERITY.get(level) < this.LOG_LEVEL_SEVERITY.get(currentLevel) : false;
|
|
48
|
+
};
|
|
49
|
+
this._capabilities = options?.capabilities ?? {};
|
|
50
|
+
this._instructions = options?.instructions;
|
|
51
|
+
this._jsonSchemaValidator = options?.jsonSchemaValidator ?? new AjvJsonSchemaValidator();
|
|
52
|
+
this.setRequestHandler(InitializeRequestSchema, request => this._oninitialize(request));
|
|
53
|
+
this.setNotificationHandler(InitializedNotificationSchema, () => this.oninitialized?.());
|
|
54
|
+
if (this._capabilities.logging) {
|
|
55
|
+
this.setRequestHandler(SetLevelRequestSchema, async (request, extra) => {
|
|
56
|
+
const transportSessionId = extra.sessionId || extra.requestInfo?.headers['mcp-session-id'] || undefined;
|
|
57
|
+
const { level } = request.params;
|
|
58
|
+
const parseResult = LoggingLevelSchema.safeParse(level);
|
|
59
|
+
if (parseResult.success) {
|
|
60
|
+
this._loggingLevels.set(transportSessionId, parseResult.data);
|
|
61
|
+
}
|
|
62
|
+
return {};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Access experimental features.
|
|
68
|
+
*
|
|
69
|
+
* WARNING: These APIs are experimental and may change without notice.
|
|
70
|
+
*
|
|
71
|
+
* @experimental
|
|
72
|
+
*/
|
|
73
|
+
get experimental() {
|
|
74
|
+
if (!this._experimental) {
|
|
75
|
+
this._experimental = {
|
|
76
|
+
tasks: new ExperimentalServerTasks(this)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return this._experimental;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Registers new capabilities. This can only be called before connecting to a transport.
|
|
83
|
+
*
|
|
84
|
+
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
|
|
85
|
+
*/
|
|
86
|
+
registerCapabilities(capabilities) {
|
|
87
|
+
if (this.transport) {
|
|
88
|
+
throw new Error('Cannot register capabilities after connecting to transport');
|
|
89
|
+
}
|
|
90
|
+
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Override request handler registration to enforce server-side validation for tools/call.
|
|
94
|
+
*/
|
|
95
|
+
setRequestHandler(requestSchema, handler) {
|
|
96
|
+
const shape = getObjectShape(requestSchema);
|
|
97
|
+
const methodSchema = shape?.method;
|
|
98
|
+
if (!methodSchema) {
|
|
99
|
+
throw new Error('Schema is missing a method literal');
|
|
100
|
+
}
|
|
101
|
+
// Extract literal value using type-safe property access
|
|
102
|
+
let methodValue;
|
|
103
|
+
if (isZ4Schema(methodSchema)) {
|
|
104
|
+
const v4Schema = methodSchema;
|
|
105
|
+
const v4Def = v4Schema._zod?.def;
|
|
106
|
+
methodValue = v4Def?.value ?? v4Schema.value;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const v3Schema = methodSchema;
|
|
110
|
+
const legacyDef = v3Schema._def;
|
|
111
|
+
methodValue = legacyDef?.value ?? v3Schema.value;
|
|
112
|
+
}
|
|
113
|
+
if (typeof methodValue !== 'string') {
|
|
114
|
+
throw new Error('Schema method literal must be a string');
|
|
115
|
+
}
|
|
116
|
+
const method = methodValue;
|
|
117
|
+
if (method === 'tools/call') {
|
|
118
|
+
const wrappedHandler = async (request, extra) => {
|
|
119
|
+
const validatedRequest = safeParse(CallToolRequestSchema, request);
|
|
120
|
+
if (!validatedRequest.success) {
|
|
121
|
+
const errorMessage = validatedRequest.error instanceof Error ? validatedRequest.error.message : String(validatedRequest.error);
|
|
122
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid tools/call request: ${errorMessage}`);
|
|
123
|
+
}
|
|
124
|
+
const { params } = validatedRequest.data;
|
|
125
|
+
const result = await Promise.resolve(handler(request, extra));
|
|
126
|
+
// When task creation is requested, validate and return CreateTaskResult
|
|
127
|
+
if (params.task) {
|
|
128
|
+
const taskValidationResult = safeParse(CreateTaskResultSchema, result);
|
|
129
|
+
if (!taskValidationResult.success) {
|
|
130
|
+
const errorMessage = taskValidationResult.error instanceof Error
|
|
131
|
+
? taskValidationResult.error.message
|
|
132
|
+
: String(taskValidationResult.error);
|
|
133
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid task creation result: ${errorMessage}`);
|
|
134
|
+
}
|
|
135
|
+
return taskValidationResult.data;
|
|
136
|
+
}
|
|
137
|
+
// For non-task requests, validate against CallToolResultSchema
|
|
138
|
+
const validationResult = safeParse(CallToolResultSchema, result);
|
|
139
|
+
if (!validationResult.success) {
|
|
140
|
+
const errorMessage = validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
|
|
141
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid tools/call result: ${errorMessage}`);
|
|
142
|
+
}
|
|
143
|
+
return validationResult.data;
|
|
144
|
+
};
|
|
145
|
+
// Install the wrapped handler
|
|
146
|
+
return super.setRequestHandler(requestSchema, wrappedHandler);
|
|
147
|
+
}
|
|
148
|
+
// Other handlers use default behavior
|
|
149
|
+
return super.setRequestHandler(requestSchema, handler);
|
|
150
|
+
}
|
|
151
|
+
assertCapabilityForMethod(method) {
|
|
152
|
+
switch (method) {
|
|
153
|
+
case 'sampling/createMessage':
|
|
154
|
+
if (!this._clientCapabilities?.sampling) {
|
|
155
|
+
throw new Error(`Client does not support sampling (required for ${method})`);
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
case 'elicitation/create':
|
|
159
|
+
if (!this._clientCapabilities?.elicitation) {
|
|
160
|
+
throw new Error(`Client does not support elicitation (required for ${method})`);
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
case 'roots/list':
|
|
164
|
+
if (!this._clientCapabilities?.roots) {
|
|
165
|
+
throw new Error(`Client does not support listing roots (required for ${method})`);
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
case 'ping':
|
|
169
|
+
// No specific capability required for ping
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
assertNotificationCapability(method) {
|
|
174
|
+
switch (method) {
|
|
175
|
+
case 'notifications/message':
|
|
176
|
+
if (!this._capabilities.logging) {
|
|
177
|
+
throw new Error(`Server does not support logging (required for ${method})`);
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case 'notifications/resources/updated':
|
|
181
|
+
case 'notifications/resources/list_changed':
|
|
182
|
+
if (!this._capabilities.resources) {
|
|
183
|
+
throw new Error(`Server does not support notifying about resources (required for ${method})`);
|
|
184
|
+
}
|
|
185
|
+
break;
|
|
186
|
+
case 'notifications/tools/list_changed':
|
|
187
|
+
if (!this._capabilities.tools) {
|
|
188
|
+
throw new Error(`Server does not support notifying of tool list changes (required for ${method})`);
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case 'notifications/prompts/list_changed':
|
|
192
|
+
if (!this._capabilities.prompts) {
|
|
193
|
+
throw new Error(`Server does not support notifying of prompt list changes (required for ${method})`);
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
case 'notifications/elicitation/complete':
|
|
197
|
+
if (!this._clientCapabilities?.elicitation?.url) {
|
|
198
|
+
throw new Error(`Client does not support URL elicitation (required for ${method})`);
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case 'notifications/cancelled':
|
|
202
|
+
// Cancellation notifications are always allowed
|
|
203
|
+
break;
|
|
204
|
+
case 'notifications/progress':
|
|
205
|
+
// Progress notifications are always allowed
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
assertRequestHandlerCapability(method) {
|
|
210
|
+
// Task handlers are registered in Protocol constructor before _capabilities is initialized
|
|
211
|
+
// Skip capability check for task methods during initialization
|
|
212
|
+
if (!this._capabilities) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
switch (method) {
|
|
216
|
+
case 'completion/complete':
|
|
217
|
+
if (!this._capabilities.completions) {
|
|
218
|
+
throw new Error(`Server does not support completions (required for ${method})`);
|
|
219
|
+
}
|
|
220
|
+
break;
|
|
221
|
+
case 'logging/setLevel':
|
|
222
|
+
if (!this._capabilities.logging) {
|
|
223
|
+
throw new Error(`Server does not support logging (required for ${method})`);
|
|
224
|
+
}
|
|
225
|
+
break;
|
|
226
|
+
case 'prompts/get':
|
|
227
|
+
case 'prompts/list':
|
|
228
|
+
if (!this._capabilities.prompts) {
|
|
229
|
+
throw new Error(`Server does not support prompts (required for ${method})`);
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
case 'resources/list':
|
|
233
|
+
case 'resources/templates/list':
|
|
234
|
+
case 'resources/read':
|
|
235
|
+
if (!this._capabilities.resources) {
|
|
236
|
+
throw new Error(`Server does not support resources (required for ${method})`);
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case 'tools/call':
|
|
240
|
+
case 'tools/list':
|
|
241
|
+
if (!this._capabilities.tools) {
|
|
242
|
+
throw new Error(`Server does not support tools (required for ${method})`);
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
case 'tasks/get':
|
|
246
|
+
case 'tasks/list':
|
|
247
|
+
case 'tasks/result':
|
|
248
|
+
case 'tasks/cancel':
|
|
249
|
+
if (!this._capabilities.tasks) {
|
|
250
|
+
throw new Error(`Server does not support tasks capability (required for ${method})`);
|
|
251
|
+
}
|
|
252
|
+
break;
|
|
253
|
+
case 'ping':
|
|
254
|
+
case 'initialize':
|
|
255
|
+
// No specific capability required for these methods
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
assertTaskCapability(method) {
|
|
260
|
+
assertClientRequestTaskCapability(this._clientCapabilities?.tasks?.requests, method, 'Client');
|
|
261
|
+
}
|
|
262
|
+
assertTaskHandlerCapability(method) {
|
|
263
|
+
// Task handlers are registered in Protocol constructor before _capabilities is initialized
|
|
264
|
+
// Skip capability check for task methods during initialization
|
|
265
|
+
if (!this._capabilities) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
assertToolsCallTaskCapability(this._capabilities.tasks?.requests, method, 'Server');
|
|
269
|
+
}
|
|
270
|
+
async _oninitialize(request) {
|
|
271
|
+
const requestedVersion = request.params.protocolVersion;
|
|
272
|
+
this._clientCapabilities = request.params.capabilities;
|
|
273
|
+
this._clientVersion = request.params.clientInfo;
|
|
274
|
+
const protocolVersion = SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : LATEST_PROTOCOL_VERSION;
|
|
275
|
+
return {
|
|
276
|
+
protocolVersion,
|
|
277
|
+
capabilities: this.getCapabilities(),
|
|
278
|
+
serverInfo: this._serverInfo,
|
|
279
|
+
...(this._instructions && { instructions: this._instructions })
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
284
|
+
*/
|
|
285
|
+
getClientCapabilities() {
|
|
286
|
+
return this._clientCapabilities;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
290
|
+
*/
|
|
291
|
+
getClientVersion() {
|
|
292
|
+
return this._clientVersion;
|
|
293
|
+
}
|
|
294
|
+
getCapabilities() {
|
|
295
|
+
return this._capabilities;
|
|
296
|
+
}
|
|
297
|
+
async ping() {
|
|
298
|
+
return this.request({ method: 'ping' }, EmptyResultSchema);
|
|
299
|
+
}
|
|
300
|
+
// Implementation
|
|
301
|
+
async createMessage(params, options) {
|
|
302
|
+
// Capability check - only required when tools/toolChoice are provided
|
|
303
|
+
if (params.tools || params.toolChoice) {
|
|
304
|
+
if (!this._clientCapabilities?.sampling?.tools) {
|
|
305
|
+
throw new Error('Client does not support sampling tools capability.');
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Message structure validation - always validate tool_use/tool_result pairs.
|
|
309
|
+
// These may appear even without tools/toolChoice in the current request when
|
|
310
|
+
// a previous sampling request returned tool_use and this is a follow-up with results.
|
|
311
|
+
if (params.messages.length > 0) {
|
|
312
|
+
const lastMessage = params.messages[params.messages.length - 1];
|
|
313
|
+
const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [lastMessage.content];
|
|
314
|
+
const hasToolResults = lastContent.some(c => c.type === 'tool_result');
|
|
315
|
+
const previousMessage = params.messages.length > 1 ? params.messages[params.messages.length - 2] : undefined;
|
|
316
|
+
const previousContent = previousMessage
|
|
317
|
+
? Array.isArray(previousMessage.content)
|
|
318
|
+
? previousMessage.content
|
|
319
|
+
: [previousMessage.content]
|
|
320
|
+
: [];
|
|
321
|
+
const hasPreviousToolUse = previousContent.some(c => c.type === 'tool_use');
|
|
322
|
+
if (hasToolResults) {
|
|
323
|
+
if (lastContent.some(c => c.type !== 'tool_result')) {
|
|
324
|
+
throw new Error('The last message must contain only tool_result content if any is present');
|
|
325
|
+
}
|
|
326
|
+
if (!hasPreviousToolUse) {
|
|
327
|
+
throw new Error('tool_result blocks are not matching any tool_use from the previous message');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (hasPreviousToolUse) {
|
|
331
|
+
const toolUseIds = new Set(previousContent.filter(c => c.type === 'tool_use').map(c => c.id));
|
|
332
|
+
const toolResultIds = new Set(lastContent.filter(c => c.type === 'tool_result').map(c => c.toolUseId));
|
|
333
|
+
if (toolUseIds.size !== toolResultIds.size || ![...toolUseIds].every(id => toolResultIds.has(id))) {
|
|
334
|
+
throw new Error('ids of tool_result blocks and tool_use blocks from previous message do not match');
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
// Use different schemas based on whether tools are provided
|
|
339
|
+
if (params.tools) {
|
|
340
|
+
return this.request({ method: 'sampling/createMessage', params }, CreateMessageResultWithToolsSchema, options);
|
|
341
|
+
}
|
|
342
|
+
return this.request({ method: 'sampling/createMessage', params }, CreateMessageResultSchema, options);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Creates an elicitation request for the given parameters.
|
|
346
|
+
* For backwards compatibility, `mode` may be omitted for form requests and will default to `'form'`.
|
|
347
|
+
* @param params The parameters for the elicitation request.
|
|
348
|
+
* @param options Optional request options.
|
|
349
|
+
* @returns The result of the elicitation request.
|
|
350
|
+
*/
|
|
351
|
+
async elicitInput(params, options) {
|
|
352
|
+
const mode = (params.mode ?? 'form');
|
|
353
|
+
switch (mode) {
|
|
354
|
+
case 'url': {
|
|
355
|
+
if (!this._clientCapabilities?.elicitation?.url) {
|
|
356
|
+
throw new Error('Client does not support url elicitation.');
|
|
357
|
+
}
|
|
358
|
+
const urlParams = params;
|
|
359
|
+
return this.request({ method: 'elicitation/create', params: urlParams }, ElicitResultSchema, options);
|
|
360
|
+
}
|
|
361
|
+
case 'form': {
|
|
362
|
+
if (!this._clientCapabilities?.elicitation?.form) {
|
|
363
|
+
throw new Error('Client does not support form elicitation.');
|
|
364
|
+
}
|
|
365
|
+
const formParams = params.mode === 'form' ? params : { ...params, mode: 'form' };
|
|
366
|
+
const result = await this.request({ method: 'elicitation/create', params: formParams }, ElicitResultSchema, options);
|
|
367
|
+
if (result.action === 'accept' && result.content && formParams.requestedSchema) {
|
|
368
|
+
try {
|
|
369
|
+
const validator = this._jsonSchemaValidator.getValidator(formParams.requestedSchema);
|
|
370
|
+
const validationResult = validator(result.content);
|
|
371
|
+
if (!validationResult.valid) {
|
|
372
|
+
throw new McpError(ErrorCode.InvalidParams, `Elicitation response content does not match requested schema: ${validationResult.errorMessage}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
if (error instanceof McpError) {
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
throw new McpError(ErrorCode.InternalError, `Error validating elicitation response: ${error instanceof Error ? error.message : String(error)}`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Creates a reusable callback that, when invoked, will send a `notifications/elicitation/complete`
|
|
388
|
+
* notification for the specified elicitation ID.
|
|
389
|
+
*
|
|
390
|
+
* @param elicitationId The ID of the elicitation to mark as complete.
|
|
391
|
+
* @param options Optional notification options. Useful when the completion notification should be related to a prior request.
|
|
392
|
+
* @returns A function that emits the completion notification when awaited.
|
|
393
|
+
*/
|
|
394
|
+
createElicitationCompletionNotifier(elicitationId, options) {
|
|
395
|
+
if (!this._clientCapabilities?.elicitation?.url) {
|
|
396
|
+
throw new Error('Client does not support URL elicitation (required for notifications/elicitation/complete)');
|
|
397
|
+
}
|
|
398
|
+
return () => this.notification({
|
|
399
|
+
method: 'notifications/elicitation/complete',
|
|
400
|
+
params: {
|
|
401
|
+
elicitationId
|
|
402
|
+
}
|
|
403
|
+
}, options);
|
|
404
|
+
}
|
|
405
|
+
async listRoots(params, options) {
|
|
406
|
+
return this.request({ method: 'roots/list', params }, ListRootsResultSchema, options);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Sends a logging message to the client, if connected.
|
|
410
|
+
* Note: You only need to send the parameters object, not the entire JSON RPC message
|
|
411
|
+
* @see LoggingMessageNotification
|
|
412
|
+
* @param params
|
|
413
|
+
* @param sessionId optional for stateless and backward compatibility
|
|
414
|
+
*/
|
|
415
|
+
async sendLoggingMessage(params, sessionId) {
|
|
416
|
+
if (this._capabilities.logging) {
|
|
417
|
+
if (!this.isMessageIgnored(params.level, sessionId)) {
|
|
418
|
+
return this.notification({ method: 'notifications/message', params });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
async sendResourceUpdated(params) {
|
|
423
|
+
return this.notification({
|
|
424
|
+
method: 'notifications/resources/updated',
|
|
425
|
+
params
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
async sendResourceListChanged() {
|
|
429
|
+
return this.notification({
|
|
430
|
+
method: 'notifications/resources/list_changed'
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
async sendToolListChanged() {
|
|
434
|
+
return this.notification({ method: 'notifications/tools/list_changed' });
|
|
435
|
+
}
|
|
436
|
+
async sendPromptListChanged() {
|
|
437
|
+
return this.notification({ method: 'notifications/prompts/list_changed' });
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
//# sourceMappingURL=index.js.map
|