tmcp 1.8.2 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmcp",
3
- "version": "1.8.2",
3
+ "version": "1.9.0",
4
4
  "description": "The main tmcp library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @import { JSONRPCRequest, JSONRPCParams } from "json-rpc-2.0";
5
5
  * @import { ExtractURITemplateVariables } from "./internal/uri-template.js";
6
6
  * @import { CallToolResult, ReadResourceResult, GetPromptResult, ClientCapabilities as ClientCapabilitiesType, JSONRPCRequest as JSONRPCRequestType, JSONRPCResponse, CreateMessageRequestParams, CreateMessageResult, Resource, LoggingLevel, ToolAnnotations } from "./validation/index.js";
7
- * @import { Tool, Completion, Prompt, StoredResource, ServerOptions, ServerInfo, SubscriptionsKeys, McpEvents } from "./internal/internal.js";
7
+ * @import { Tool, Completion, Prompt, StoredResource, ServerOptions, ServerInfo, SubscriptionsKeys, McpEvents, Context as ContextType } from "./internal/internal.js";
8
8
  */
9
9
  import { JSONRPCClient, JSONRPCServer } from 'json-rpc-2.0';
10
10
  import { AsyncLocalStorage } from 'node:async_hooks';
@@ -29,6 +29,25 @@ import {
29
29
  } from './validation/version.js';
30
30
  import { should_version_negotiation_fail } from './validation/version.js';
31
31
 
32
+ /**
33
+ * Information about a validated access token, provided to request handlers.
34
+ * @typedef {Object} AuthInfo
35
+ * @property {string} token - The access token.
36
+ * @property {string} clientId - The client ID associated with this token.
37
+ * @property {string[]} scopes - Scopes associated with this token.
38
+ * @property {number} [expiresAt] - When the token expires (in seconds since epoch).
39
+ * @property {URL} [resource] - The RFC 8707 resource server identifier for which this token is valid.
40
+ * If set, this MUST match the MCP server's resource identifier (minus hash fragment).
41
+ * @property {Record<string, unknown>} [extra] - Additional data associated with the token.
42
+ * This field should be used for any additional data that needs to be attached to the auth info.
43
+ */
44
+
45
+ /**
46
+ * @typedef {Object} Context
47
+ * @property {string} [sessionId]
48
+ * @property {AuthInfo} [auth]
49
+ */
50
+
32
51
  /**
33
52
  * @type {SqidsType | undefined}
34
53
  */
@@ -110,9 +129,9 @@ export class McpServer {
110
129
  };
111
130
 
112
131
  /**
113
- * @type {AsyncLocalStorage<string | undefined>}
132
+ * @type {AsyncLocalStorage<Context>}
114
133
  */
115
- #session_storage = new AsyncLocalStorage();
134
+ #ctx_storage = new AsyncLocalStorage();
116
135
 
117
136
  /**
118
137
  * @type {Map<string|undefined, ClientCapabilities>}
@@ -236,7 +255,11 @@ export class McpServer {
236
255
  }
237
256
 
238
257
  get #session_id() {
239
- return this.#session_storage.getStore();
258
+ return this.#ctx_storage.getStore()?.sessionId;
259
+ }
260
+
261
+ get ctx() {
262
+ return this.#ctx_storage.getStore() ?? {};
240
263
  }
241
264
 
242
265
  get #client_capabilities() {
@@ -690,10 +713,10 @@ export class McpServer {
690
713
  }
691
714
  /**
692
715
  * @param {JSONRPCResponse | JSONRPCRequest} message
693
- * @param {string} [session_id]
716
+ * @param {Context} [ctx]
694
717
  * @returns {ReturnType<JSONRPCServer['receive']> | ReturnType<JSONRPCClient['receive'] | undefined>}
695
718
  */
696
- receive(message, session_id) {
719
+ receive(message, ctx) {
697
720
  // Validate the message first
698
721
  const validated_message = v.safeParse(
699
722
  v.union([JSONRPCRequestSchema, JSONRPCNotificationSchema]),
@@ -707,15 +730,15 @@ export class McpServer {
707
730
  // to each client that didn't explicitly set a log level too
708
731
  if (
709
732
  !!this.#options.capabilities?.logging &&
710
- !this.#session_log_levels.has(session_id)
733
+ !this.#session_log_levels.has(ctx?.sessionId)
711
734
  ) {
712
735
  this.#session_log_levels.set(
713
- session_id,
736
+ ctx?.sessionId,
714
737
  this.#options.logging?.default ?? 'info',
715
738
  );
716
739
  }
717
- return this.#session_storage.run(
718
- session_id,
740
+ return this.#ctx_storage.run(
741
+ ctx ?? {},
719
742
  async () =>
720
743
  await this.#server.receive(validated_message.output),
721
744
  );
@@ -723,7 +746,7 @@ export class McpServer {
723
746
  // It's a response - handle with client
724
747
  const validated_response = v.parse(JSONRPCResponseSchema, message);
725
748
  this.#lazyily_create_client();
726
- return this.#session_storage.run(session_id, async () =>
749
+ return this.#ctx_storage.run(ctx ?? {}, async () =>
727
750
  this.#client?.receive(validated_response),
728
751
  );
729
752
  }
@@ -90,4 +90,4 @@ export type McpEvents = {
90
90
  };
91
91
  }) => void;
92
92
  initialize: (initialize_request: InitializeRequestParams) => void;
93
- };
93
+ };
@@ -11,6 +11,7 @@ declare module 'tmcp' {
11
11
  uri: string;
12
12
  name?: string;
13
13
  }>;
14
+ get ctx(): Context;
14
15
  currentClientCapabilities(): {
15
16
  experimental?: {} | undefined;
16
17
  sampling?: {} | undefined;
@@ -54,7 +55,7 @@ declare module 'tmcp' {
54
55
  list?: () => Promise<Array<Resource>> | Array<Resource>;
55
56
  }, execute: (uri: string, params: Record<TVariables, string | string[]>) => Promise<ReadResourceResult> | ReadResourceResult): void;
56
57
 
57
- receive(message: JSONRPCResponse | JSONRPCRequest, session_id?: string): ReturnType<JSONRPCServer["receive"]> | ReturnType<JSONRPCClient["receive"] | undefined>;
58
+ receive(message: JSONRPCResponse | JSONRPCRequest, ctx?: Context): ReturnType<JSONRPCServer["receive"]> | ReturnType<JSONRPCClient["receive"] | undefined>;
58
59
  /**
59
60
  * Send a notification for subscriptions
60
61
  * */
@@ -76,6 +77,41 @@ declare module 'tmcp' {
76
77
  log(level: LoggingLevel, data: unknown, logger?: string): void;
77
78
  #private;
78
79
  }
80
+ /**
81
+ * Information about a validated access token, provided to request handlers.
82
+ */
83
+ export type AuthInfo = {
84
+ /**
85
+ * - The access token.
86
+ */
87
+ token: string;
88
+ /**
89
+ * - The client ID associated with this token.
90
+ */
91
+ clientId: string;
92
+ /**
93
+ * - Scopes associated with this token.
94
+ */
95
+ scopes: string[];
96
+ /**
97
+ * - When the token expires (in seconds since epoch).
98
+ */
99
+ expiresAt?: number | undefined;
100
+ /**
101
+ * - The RFC 8707 resource server identifier for which this token is valid.
102
+ * If set, this MUST match the MCP server's resource identifier (minus hash fragment).
103
+ */
104
+ resource?: URL | undefined;
105
+ /**
106
+ * - Additional data associated with the token.
107
+ * This field should be used for any additional data that needs to be attached to the auth info.
108
+ */
109
+ extra?: Record<string, unknown> | undefined;
110
+ };
111
+ export type Context = {
112
+ sessionId?: string | undefined;
113
+ auth?: AuthInfo | undefined;
114
+ };
79
115
  export type ClientCapabilities = ClientCapabilities_1;
80
116
  type Completion = (
81
117
  query: string,
@@ -56,6 +56,6 @@
56
56
  null,
57
57
  null
58
58
  ],
59
- "mappings": ";;;;;cAmEaA,SAASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aCo4CsCC,kBAAkBA;MC96ClEC,UAAUA;;;;;MAsCVC,aAAaA;;;;;;;;;;;;;MAabC,UAAUA;;;;;;MAMVC,iBAAiBA;;MAEjBC,SAASA;;;;;;;;;;;;ODgBRC,qBAAqBA;;;;;;;;;;;;;;OAuGrBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;OA6BxBC,6BAA6BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuB7BC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmMxBC,cAAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4GdC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8PxBC,qBAAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA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