tmcp 1.19.3 → 1.20.0-next.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.
@@ -11,7 +11,10 @@ import {
11
11
  ServerCapabilities,
12
12
  LoggingLevel,
13
13
  ToolAnnotations,
14
- Icons
14
+ Icons,
15
+ McpError,
16
+ SubscriptionFilter,
17
+ RequestId,
15
18
  } from '../validation/index.js';
16
19
  import { ExtractURITemplateVariables } from './uri-template.js';
17
20
 
@@ -22,52 +25,173 @@ declare const created_template: unique symbol;
22
25
 
23
26
  export type AllSame<T, U> = [T] extends [U] ? true : false;
24
27
 
25
- export type PromptOptions<TSchema extends StandardSchemaV1 | undefined = undefined> = {
26
- name: string;
27
- description: string;
28
- title?: string;
29
- enabled?: ()=>boolean | Promise<boolean>;
30
- schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never;
31
- complete?: NoInfer<TSchema extends undefined ? never : Partial<Record<keyof (StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>), Completion>>>
32
- } & Icons
28
+ /** @internal Work data used only while one request is running. */
29
+ export type MrtrState = {
30
+ input_responses: Record<string, unknown> | undefined;
31
+ incoming_state: unknown;
32
+ ordinal: number;
33
+ used_keys: Set<string>;
34
+ pending: Map<
35
+ string,
36
+ Promise<{ method: string; params: Record<string, unknown> }>
37
+ >;
38
+ consumed_responses: Map<string, unknown>;
39
+ outgoing_state: unknown;
40
+ registration:
41
+ | { kind: string; name: string; replayable: boolean }
42
+ | undefined;
43
+ input_error: McpError | undefined;
44
+ signal: Error;
45
+ signal_at_boundary: boolean;
46
+ };
47
+
48
+ export type SubscriptionOrigin = string;
49
+
50
+ export type Subscription = {
51
+ id: RequestId;
52
+ origin: SubscriptionOrigin;
53
+ filters: SubscriptionFilter;
54
+ };
55
+
56
+ export type SubscriptionCallbacks = {
57
+ acknowledge: () => void | Promise<void>;
58
+ send: (notification: JSONRPCRequest) => void | Promise<void>;
59
+ close: (reason: 'closed' | 'cancelled') => void | Promise<void>;
60
+ };
61
+
62
+ export type SubscriptionManager = {
63
+ create(
64
+ subscription: Subscription,
65
+ callbacks: SubscriptionCallbacks,
66
+ ): boolean | Promise<boolean>;
67
+ send(notification: JSONRPCRequest): void | Promise<void>;
68
+ close(
69
+ id: RequestId,
70
+ origin: SubscriptionOrigin,
71
+ reason: 'closed' | 'cancelled',
72
+ ): boolean | Promise<boolean>;
73
+ closeAll(
74
+ origin?: SubscriptionOrigin,
75
+ reason?: 'closed' | 'cancelled',
76
+ ): void | Promise<void>;
77
+ };
33
78
 
34
- export type ToolOptions<TSchema extends StandardSchemaV1 | undefined = undefined, TOutputSchema extends StandardSchemaV1 | undefined = undefined> = {
79
+ export type Replayable = {
80
+ /**
81
+ * Allow tmcp to run this handler again after asking the client for input.
82
+ *
83
+ * For standalone requests, the server cannot pause and wait for the client.
84
+ * It returns the questions to the client, which then retries the original
85
+ * request with the answers. The handler starts again FROM THE TOP on every
86
+ * retry. This means work done before an input call, such as database writes,
87
+ * emails, or payments, may happen more than once. Set `replayable: true`
88
+ * only when that work is safe to repeat or is delayed until all answers are
89
+ * available.
90
+ *
91
+ * Without this flag, tmcp returns an error rather than risk repeating work.
92
+ * This is a tmcp safety check, not an MCP requirement. Requests using an
93
+ * open client session are unaffected because their handlers do not restart.
94
+ */
95
+ replayable?: boolean;
96
+ };
97
+
98
+ export type PromptOptions<
99
+ TSchema extends StandardSchemaV1 | undefined = undefined,
100
+ > = {
101
+ name: string;
102
+ description: string;
103
+ title?: string;
104
+ enabled?: () => boolean | Promise<boolean>;
105
+ schema?: StandardSchemaV1.InferInput<
106
+ TSchema extends undefined ? never : TSchema
107
+ > extends Record<string, unknown>
108
+ ? TSchema
109
+ : never;
110
+ complete?: NoInfer<
111
+ TSchema extends undefined
112
+ ? never
113
+ : Partial<
114
+ Record<
115
+ keyof StandardSchemaV1.InferInput<
116
+ TSchema extends undefined ? never : TSchema
117
+ >,
118
+ Completion
119
+ >
120
+ >
121
+ >;
122
+ } & Icons &
123
+ Replayable;
124
+
125
+ export type ToolOptions<
126
+ TSchema extends StandardSchemaV1 | undefined = undefined,
127
+ TOutputSchema extends StandardSchemaV1 | undefined = undefined,
128
+ > = {
35
129
  name: string;
36
130
  _meta?: Record<string, any>;
37
131
  description: string;
38
132
  title?: string;
39
133
  enabled?: () => boolean | Promise<boolean>;
40
- schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never;
41
- outputSchema?: StandardSchemaV1.InferOutput<TOutputSchema extends undefined ? never : TOutputSchema> extends Record<string, unknown> ? TOutputSchema : never;
134
+ schema?: StandardSchemaV1.InferInput<
135
+ TSchema extends undefined ? never : TSchema
136
+ > extends Record<string, unknown>
137
+ ? TSchema
138
+ : never;
139
+ outputSchema?: StandardSchemaV1.InferOutput<
140
+ TOutputSchema extends undefined ? never : TOutputSchema
141
+ > extends Record<string, unknown>
142
+ ? TOutputSchema
143
+ : never;
42
144
  annotations?: ToolAnnotations;
43
- } & Icons;
145
+ } & Icons &
146
+ Replayable;
44
147
 
45
- export type ResourceOptions = {
148
+ export type ResourceOptions = {
46
149
  name: string;
47
- description: string;
48
- title?: string;
150
+ description: string;
151
+ title?: string;
49
152
  uri: string;
50
153
  mimeType?: string;
51
- enabled?: ()=>boolean | Promise<boolean>;
52
- } & Icons
154
+ enabled?: () => boolean | Promise<boolean>;
155
+ } & Icons &
156
+ Replayable;
53
157
 
54
- export type TemplateOptions<TUri extends string = string, TVariables extends ExtractURITemplateVariables<TUri> = ExtractURITemplateVariables<TUri>> = {
158
+ export type TemplateOptions<
159
+ TUri extends string = string,
160
+ TVariables extends ExtractURITemplateVariables<TUri> =
161
+ ExtractURITemplateVariables<TUri>,
162
+ > = {
55
163
  name: string;
56
164
  description: string;
57
165
  title?: string;
58
166
  mimeType?: string;
59
- enabled?: ()=>boolean | Promise<boolean>;
167
+ enabled?: () => boolean | Promise<boolean>;
60
168
  uri: TUri;
61
- complete?: NoInfer<TVariables extends never ? never : Partial<Record<TVariables, Completion>>>;
62
- list?: () => Promise<Array<Resource>> | Array<Resource>
63
- } & Icons
169
+ complete?: NoInfer<
170
+ TVariables extends never
171
+ ? never
172
+ : Partial<Record<TVariables, Completion>>
173
+ >;
174
+ list?: () => Promise<Array<Resource>> | Array<Resource>;
175
+ } & Icons &
176
+ Replayable;
64
177
 
65
- export type CreatedTool<TSchema extends StandardSchemaV1 | undefined = undefined, TOutputSchema extends StandardSchemaV1 | undefined = undefined> = ToolOptions<TSchema, TOutputSchema> & { [created_tool]: created_tool };
66
- export type CreatedPrompt<TSchema extends StandardSchemaV1 | undefined = undefined> = PromptOptions<TSchema> & { [created_prompt]: created_prompt };
67
- export type CreatedResource = ResourceOptions & { [created_resource]: created_resource };
68
- export type CreatedTemplate<TUri extends string = string> = TemplateOptions<TUri> & { [created_template]: created_template };
178
+ export type CreatedTool<
179
+ TSchema extends StandardSchemaV1 | undefined = undefined,
180
+ TOutputSchema extends StandardSchemaV1 | undefined = undefined,
181
+ > = ToolOptions<TSchema, TOutputSchema> & { [created_tool]: created_tool };
182
+ export type CreatedPrompt<
183
+ TSchema extends StandardSchemaV1 | undefined = undefined,
184
+ > = PromptOptions<TSchema> & { [created_prompt]: created_prompt };
185
+ export type CreatedResource = ResourceOptions & {
186
+ [created_resource]: created_resource;
187
+ };
188
+ export type CreatedTemplate<TUri extends string = string> =
189
+ TemplateOptions<TUri> & { [created_template]: created_template };
69
190
 
70
- export type Tool<TSchema extends StandardSchemaV1 = StandardSchemaV1<any>, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<any>> = ToolOptions<TSchema, TOutputSchema> & {
191
+ export type Tool<
192
+ TSchema extends StandardSchemaV1 = StandardSchemaV1<any>,
193
+ TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<any>,
194
+ > = ToolOptions<TSchema, TOutputSchema> & {
71
195
  execute: (
72
196
  input?: StandardSchemaV1.InferInput<TSchema>,
73
197
  ) => Promise<CallToolResult> | CallToolResult;
@@ -78,29 +202,41 @@ export type Completion = (
78
202
  context: { arguments: Record<string, string> },
79
203
  ) => CompleteResult | Promise<CompleteResult>;
80
204
 
81
- export type Prompt<TSchema extends StandardSchemaV1 = StandardSchemaV1<any>> = PromptOptions<TSchema> & {
82
- execute: (
83
- input?: StandardSchemaV1.InferInput<TSchema>,
84
- ) => Promise<GetPromptResult> | GetPromptResult;
85
- };
205
+ export type Prompt<TSchema extends StandardSchemaV1 = StandardSchemaV1<any>> =
206
+ PromptOptions<TSchema> & {
207
+ execute: (
208
+ input?: StandardSchemaV1.InferInput<TSchema>,
209
+ ) => Promise<GetPromptResult> | GetPromptResult;
210
+ };
86
211
 
87
212
  export type StoredResource =
88
- | TemplateOptions<string, string> & {
213
+ | (TemplateOptions<string, string> & {
89
214
  template: true;
90
- list_resources?: () =>
91
- | Promise<Array<Resource>>
92
- | Array<Resource>;
215
+ list_resources?: () => Promise<Array<Resource>> | Array<Resource>;
93
216
  execute: (
94
217
  uri: string,
95
218
  params: Record<string, string | string[]>,
96
219
  ) => Promise<ReadResourceResult> | ReadResourceResult;
97
- }
98
- | ResourceOptions & {
220
+ })
221
+ | (ResourceOptions & {
99
222
  template: false;
100
223
  execute: (
101
224
  uri: string,
102
225
  ) => Promise<ReadResourceResult> | ReadResourceResult;
103
- };
226
+ });
227
+
228
+ export type CachePolicy = {
229
+ /**
230
+ * Time-to-live in milliseconds for cacheable results (>= 0). Defaults to 0 (no caching).
231
+ */
232
+ ttlMs?: number;
233
+ /**
234
+ * Cache scope for cacheable results. Defaults to 'private'. `public` must be
235
+ * explicitly opted into: `enabled` callbacks, auth context, and dynamic
236
+ * resource/template listings can make otherwise identical lists user-specific.
237
+ */
238
+ cacheScope?: 'public' | 'private';
239
+ };
104
240
 
105
241
  export type ServerOptions<TSchema extends StandardSchemaV1 | undefined> = {
106
242
  capabilities?: ServerCapabilities;
@@ -113,30 +249,69 @@ export type ServerOptions<TSchema extends StandardSchemaV1 | undefined> = {
113
249
  };
114
250
  logging?: {
115
251
  default: LoggingLevel;
116
- }
252
+ };
253
+ /**
254
+ * Cache policy for per-request protocol cacheable results
255
+ * (`server/discover`, `tools/list`, `prompts/list`, `resources/list`,
256
+ * `resources/read`, `resources/templates/list`). Defaults to
257
+ * `{ ttlMs: 0, cacheScope: 'private' }`. Per-method overrides win over
258
+ * the top-level defaults.
259
+ */
260
+ cache?: CachePolicy & {
261
+ methods?: Record<string, CachePolicy>;
262
+ };
263
+ /**
264
+ * Convert tmcp's retry data to text before sending it to the client, and
265
+ * restore that data when the client retries the request. The value includes
266
+ * data saved by the handler and validated answers needed to restart it from
267
+ * the beginning. Treat the value passed to `encode` as tmcp-owned data; its
268
+ * internal shape may grow in future protocol versions.
269
+ *
270
+ * SECURITY: the default uses plain JSON, so the client can read, change, or
271
+ * replace the saved data. Do not store secrets in it or use it to make
272
+ * authorization decisions. Use a protected converter if the server must
273
+ * detect changes. That converter should also tie the data to the current
274
+ * user, the original request, and a short expiry time.
275
+ *
276
+ * tmcp limits the text length before calling `decode`. `decode` may be
277
+ * asynchronous. Custom converters should also limit the restored value if
278
+ * needed, because tmcp cannot measure every application-specific value.
279
+ */
280
+ requestStateCodec?: {
281
+ encode: (state: unknown) => string | Promise<string>;
282
+ decode: (encoded: string) => unknown | Promise<unknown>;
283
+ };
117
284
  };
118
285
 
119
286
  export type ChangedArgs = {
120
- 'resource': [id: string];
121
- 'tools': [];
122
- 'prompts': [];
123
- 'resources': [];
124
- }
287
+ resource: [id: string];
288
+ tools: [];
289
+ prompts: [];
290
+ resources: [];
291
+ };
125
292
 
126
293
  type SubscriptionsKeysObj = {
127
- [K in keyof ChangedArgs as ChangedArgs[K]["length"] extends 0 ? "without_args" : "with_args"]: K
294
+ [K in keyof ChangedArgs as ChangedArgs[K]['length'] extends 0
295
+ ? 'without_args'
296
+ : 'with_args']: K;
128
297
  };
129
298
 
130
- export type SubscriptionsKeys = SubscriptionsKeysObj["with_args"];
299
+ export type SubscriptionsKeys = SubscriptionsKeysObj['with_args'];
131
300
 
132
301
  export type McpEvents = {
133
302
  send: (message: {
134
303
  request: JSONRPCRequest;
304
+ subscriptionId?: RequestId;
305
+ subscriptionOrigin?: SubscriptionOrigin;
135
306
  }) => void;
136
307
  broadcast: (message: {
137
308
  request: JSONRPCRequest;
309
+ subscriptionOnly?: boolean;
138
310
  }) => void;
139
311
  initialize: (initialize_request: InitializeRequestParams) => void;
140
- subscription: (subscriptions_request: { uri: string, action?: "add" | "remove" }) => void;
312
+ subscription: (subscriptions_request: {
313
+ uri: string;
314
+ action?: 'add' | 'remove';
315
+ }) => void;
141
316
  loglevelchange: (change: { level: LoggingLevel }) => void;
142
- };
317
+ };
@@ -0,0 +1 @@
1
+ export { is_per_request_method_allowed as isPerRequestMethodAllowed } from './validation/method-policy.js';