tmcp 1.6.1 → 1.7.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.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "The main tmcp library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.d.ts CHANGED
@@ -44,14 +44,15 @@ export class McpServer<StandardSchema extends StandardSchemaV1> {
44
44
  on<TEvent extends keyof McpEvents>(event: TEvent, callback: McpEvents[TEvent], options?: AddEventListenerOptions): void;
45
45
  /**
46
46
  * @template {StandardSchema | undefined} [TSchema=undefined]
47
- * @param {{ name: string; description: string; title?: string; schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never }} options
47
+ * @param {{ name: string; description: string; title?: string; schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never; annotations?: ToolAnnotations }} options
48
48
  * @param {TSchema extends undefined ? (()=>Promise<CallToolResult> | CallToolResult) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<CallToolResult> | CallToolResult)} execute
49
49
  */
50
- tool<TSchema extends StandardSchema | undefined = undefined>({ name, description, title, schema }: {
50
+ tool<TSchema extends StandardSchema | undefined = undefined>({ name, description, title, schema, annotations }: {
51
51
  name: string;
52
52
  description: string;
53
53
  title?: string;
54
54
  schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never;
55
+ annotations?: ToolAnnotations;
55
56
  }, execute: TSchema extends undefined ? (() => Promise<CallToolResult> | CallToolResult) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<CallToolResult> | CallToolResult)): void;
56
57
  /**
57
58
  * @template {StandardSchema | undefined} [TSchema=undefined]
@@ -129,6 +130,7 @@ export class McpServer<StandardSchema extends StandardSchemaV1> {
129
130
  export type ClientCapabilities = ClientCapabilitiesType;
130
131
  import type { StandardSchemaV1 } from "@standard-schema/spec";
131
132
  import type { McpEvents } from "./internal/internal.js";
133
+ import type { ToolAnnotations } from "./validation/index.js";
132
134
  import type { CallToolResult } from "./validation/index.js";
133
135
  import type { Completion } from "./internal/internal.js";
134
136
  import type { GetPromptResult } from "./validation/index.js";
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @import SqidsType from "sqids";
4
4
  * @import { JSONRPCRequest, JSONRPCParams } from "json-rpc-2.0";
5
5
  * @import { ExtractURITemplateVariables } from "./internal/uri-template.js";
6
- * @import { CallToolResult, ReadResourceResult, GetPromptResult, ClientCapabilities as ClientCapabilitiesType, JSONRPCRequest as JSONRPCRequestType, JSONRPCResponse, CreateMessageRequestParams, CreateMessageResult, Resource, LoggingLevel } from "./validation/index.js";
6
+ * @import { CallToolResult, ReadResourceResult, GetPromptResult, ClientCapabilities as ClientCapabilitiesType, JSONRPCRequest as JSONRPCRequestType, JSONRPCResponse, CreateMessageRequestParams, CreateMessageResult, Resource, LoggingLevel, ToolAnnotations } from "./validation/index.js";
7
7
  * @import { Tool, Completion, Prompt, StoredResource, ServerOptions, ServerInfo, SubscriptionsKeys, McpEvents } from "./internal/internal.js";
8
8
  */
9
9
  import { JSONRPCClient, JSONRPCServer } from 'json-rpc-2.0';
@@ -300,6 +300,7 @@ export class McpServer {
300
300
  name,
301
301
  title: tool.title || tool.description,
302
302
  description: tool.description,
303
+ annotations: tool.annotations,
303
304
  inputSchema: tool.schema
304
305
  ? await this.#options.adapter.toJsonSchema(
305
306
  tool.schema,
@@ -317,7 +318,7 @@ export class McpServer {
317
318
  async ({ name, arguments: args }) => {
318
319
  const tool = this.#tools.get(name);
319
320
  if (!tool) {
320
- throw new Error(`Tool ${name} not found`);
321
+ throw new McpError(-32601, `Tool ${name} not found`);
321
322
  }
322
323
  if (!tool.schema) {
323
324
  return v.parse(CallToolResultSchema, await tool.execute());
@@ -326,7 +327,8 @@ export class McpServer {
326
327
  if (validated_args instanceof Promise)
327
328
  validated_args = await validated_args;
328
329
  if (validated_args.issues) {
329
- throw new Error(
330
+ throw new McpError(
331
+ -32602,
330
332
  `Invalid arguments for tool ${name}: ${JSON.stringify(validated_args.issues)}`,
331
333
  );
332
334
  }
@@ -403,7 +405,7 @@ export class McpServer {
403
405
  async ({ name, arguments: args }) => {
404
406
  const prompt = this.#prompts.get(name);
405
407
  if (!prompt) {
406
- throw new Error(`Tool ${name} not found`);
408
+ throw new McpError(-32601, `Prompt ${name} not found`);
407
409
  }
408
410
  if (!prompt.schema) {
409
411
  return v.parse(
@@ -415,8 +417,9 @@ export class McpServer {
415
417
  if (validated_args instanceof Promise)
416
418
  validated_args = await validated_args;
417
419
  if (validated_args.issues) {
418
- throw new Error(
419
- `Invalid arguments for tool ${name}: ${JSON.stringify(validated_args.issues)}`,
420
+ throw new McpError(
421
+ -32602,
422
+ `Invalid arguments for prompt ${name}: ${JSON.stringify(validated_args.issues)}`,
420
423
  );
421
424
  }
422
425
  return v.parse(
@@ -512,12 +515,15 @@ export class McpServer {
512
515
  params = match.params;
513
516
  }
514
517
  if (!resource) {
515
- throw new Error(`Resource ${uri} not found`);
518
+ throw new McpError(-32601, `Resource ${uri} not found`);
516
519
  }
517
520
  }
518
521
  if (resource.template) {
519
522
  if (!params)
520
- throw new Error('Missing parameters for template resource');
523
+ throw new McpError(
524
+ -32602,
525
+ 'Missing parameters for template resource',
526
+ );
521
527
  return v.parse(
522
528
  ReadResourceResultSchema,
523
529
  await resource.execute(uri, params),
@@ -593,10 +599,10 @@ export class McpServer {
593
599
  }
594
600
  /**
595
601
  * @template {StandardSchema | undefined} [TSchema=undefined]
596
- * @param {{ name: string; description: string; title?: string; schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never }} options
602
+ * @param {{ name: string; description: string; title?: string; schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never; annotations?: ToolAnnotations }} options
597
603
  * @param {TSchema extends undefined ? (()=>Promise<CallToolResult> | CallToolResult) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<CallToolResult> | CallToolResult)} execute
598
604
  */
599
- tool({ name, description, title, schema }, execute) {
605
+ tool({ name, description, title, schema, annotations }, execute) {
600
606
  if (this.#options.capabilities?.tools?.listChanged) {
601
607
  this.#notify('notifications/tools/list_changed', {});
602
608
  }
@@ -605,6 +611,7 @@ export class McpServer {
605
611
  title,
606
612
  schema,
607
613
  execute,
614
+ annotations,
608
615
  });
609
616
  }
610
617
  /**
@@ -753,7 +760,7 @@ export class McpServer {
753
760
  */
754
761
  async elicitation(schema) {
755
762
  if (!this.#client_capabilities?.elicitation)
756
- throw new Error("Client doesn't support elicitation");
763
+ throw new McpError(-32601, "Client doesn't support elicitation");
757
764
 
758
765
  this.#lazyily_create_client();
759
766
  let validated_result = schema['~standard'].validate(
@@ -770,7 +777,8 @@ export class McpServer {
770
777
  if (validated_result instanceof Promise)
771
778
  validated_result = await validated_result;
772
779
  if (validated_result.issues) {
773
- throw new Error(
780
+ throw new McpError(
781
+ -32603,
774
782
  `Invalid elicitation result: ${JSON.stringify(validated_result.issues)}`,
775
783
  );
776
784
  }
@@ -784,7 +792,7 @@ export class McpServer {
784
792
  */
785
793
  async message(request) {
786
794
  if (!this.#client_capabilities?.sampling)
787
- throw new Error("Client doesn't support sampling");
795
+ throw new McpError(-32601, "Client doesn't support sampling");
788
796
 
789
797
  this.#lazyily_create_client();
790
798
 
@@ -815,7 +823,8 @@ export class McpServer {
815
823
  */
816
824
  log(level, data, logger) {
817
825
  if (!this.#options.capabilities?.logging) {
818
- throw new Error(
826
+ throw new McpError(
827
+ -32601,
819
828
  "The server doesn't support logging, please enable it in capabilities",
820
829
  );
821
830
  }
@@ -9,13 +9,15 @@ import {
9
9
  InitializeRequestParams,
10
10
  Resource,
11
11
  ServerCapabilities,
12
- LoggingLevel
12
+ LoggingLevel,
13
+ ToolAnnotations
13
14
  } from '../validation/index.js';
14
15
 
15
16
  export type Tool<TSchema extends StandardSchemaV1 = StandardSchemaV1<any>> = {
16
17
  description: string;
17
18
  schema: TSchema;
18
19
  title?: string;
20
+ annotations?: ToolAnnotations;
19
21
  execute: (
20
22
  input?: StandardSchemaV1.InferInput<TSchema>,
21
23
  ) => Promise<CallToolResult> | CallToolResult;
@@ -4315,4 +4315,5 @@ export type Resource = v.InferInput<typeof ResourceSchema>;
4315
4315
  export type JSONRPCRequest = v.InferInput<typeof JSONRPCRequestSchema>;
4316
4316
  export type JSONRPCResponse = v.InferInput<typeof JSONRPCResponseSchema>;
4317
4317
  export type LoggingLevel = v.InferInput<typeof LoggingLevelSchema>;
4318
+ export type ToolAnnotations = v.InferInput<typeof ToolAnnotationsSchema>;
4318
4319
  import * as v from 'valibot';
@@ -1530,3 +1530,6 @@ export const ServerResultSchema = v.union([
1530
1530
  /**
1531
1531
  * @typedef {v.InferInput<typeof LoggingLevelSchema>} LoggingLevel
1532
1532
  */
1533
+ /**
1534
+ * @typedef {v.InferInput<typeof ToolAnnotationsSchema>} ToolAnnotations
1535
+ */