@ttoss/http-server-mcp 0.33.1 → 0.34.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/MIGRATIONS.md +71 -0
- package/README.md +199 -177
- package/dist/index.cjs +694 -161
- package/dist/index.d.cts +307 -152
- package/dist/index.d.mts +307 -152
- package/dist/index.mjs +691 -160
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { Router } from "@ttoss/http-server";
|
|
4
4
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
-
import { CallToolResult, InMemoryServerEventBus, McpServer, McpServer as McpServer$1, McpServerFactory, RegisteredResource, ServerEvent, ServerEventBus, ServerEventBus as ServerEventBus$1, ServerNotifier, ServerNotifier as ServerNotifier$1, ToolAnnotations } from "@modelcontextprotocol/server";
|
|
5
|
+
import { CallToolResult, InMemoryServerEventBus, McpServer, McpServer as McpServer$1, McpServerFactory, RegisteredResource, ServerEvent, ServerEventBus, ServerEventBus as ServerEventBus$1, ServerNotifier, ServerNotifier as ServerNotifier$1, StandardSchemaWithJSON, ToolAnnotations } from "@modelcontextprotocol/server";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import accepts from "accepts";
|
|
8
8
|
import { AsyncLocalStorage as AsyncLocalStorage$1 } from "async_hooks";
|
|
@@ -688,10 +688,199 @@ declare const getApiHeaders: () => Record<string, string>;
|
|
|
688
688
|
/**
|
|
689
689
|
* The `Accept-Language` header of the current MCP request, or `undefined`
|
|
690
690
|
* outside a request or when the client sent none. The default locale source
|
|
691
|
-
* for `
|
|
691
|
+
* for `createToolGate`'s `i18n` option.
|
|
692
692
|
*/
|
|
693
693
|
declare const getRequestLocale: () => string | undefined;
|
|
694
694
|
//#endregion
|
|
695
|
+
//#region src/tool.d.ts
|
|
696
|
+
/**
|
|
697
|
+
* A plain JSON Schema object (draft-07 compatible) describing the shape of a
|
|
698
|
+
* tool's input or output. Forwarded verbatim over `tools/list`, so features
|
|
699
|
+
* Zod cannot express (`anyOf`, `$ref`, `pattern`, `allOf`, …) survive intact.
|
|
700
|
+
*/
|
|
701
|
+
interface JsonObjectSchema {
|
|
702
|
+
type: 'object';
|
|
703
|
+
properties?: Record<string, unknown>;
|
|
704
|
+
required?: string[];
|
|
705
|
+
[key: string]: unknown;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* A schema a tool declares: a plain JSON Schema, or a Standard Schema with
|
|
709
|
+
* JSON Schema support — Zod 4's `z.object(...)`, ArkType, or Valibot.
|
|
710
|
+
*/
|
|
711
|
+
type ToolSchema = JsonObjectSchema | StandardSchemaWithJSON;
|
|
712
|
+
/**
|
|
713
|
+
* An MCP tool: what it is and how it answers, independent of how a server
|
|
714
|
+
* exposes it. {@link registerTools} registers a list of them, either one MCP
|
|
715
|
+
* tool each or deferred behind `search` / `describe` / `call`.
|
|
716
|
+
*/
|
|
717
|
+
interface Tool {
|
|
718
|
+
/** Unique tool name. */
|
|
719
|
+
name: string;
|
|
720
|
+
/** Display name clients show instead of `name`. */
|
|
721
|
+
title?: string;
|
|
722
|
+
/** What the tool does, written for the model. */
|
|
723
|
+
description: string;
|
|
724
|
+
/**
|
|
725
|
+
* The tool's input.
|
|
726
|
+
*
|
|
727
|
+
* - A **JSON Schema** is advertised verbatim and enforced only with
|
|
728
|
+
* `validateArguments`; the handler receives the raw arguments.
|
|
729
|
+
* - A **Standard Schema** (`z.object(...)`) is always enforced; the handler
|
|
730
|
+
* receives its parsed output, defaults and coercions applied.
|
|
731
|
+
*
|
|
732
|
+
* @default { type: 'object', properties: {} }
|
|
733
|
+
*/
|
|
734
|
+
inputSchema?: ToolSchema;
|
|
735
|
+
/**
|
|
736
|
+
* Whether a JSON Schema `inputSchema` is enforced before the handler runs.
|
|
737
|
+
*
|
|
738
|
+
* Enable it once the schema describes every value the tool genuinely
|
|
739
|
+
* accepts. Schemas generated from an OpenAPI document are a common source of
|
|
740
|
+
* *incomplete* ones — a field a client may send as `null` to clear it, or
|
|
741
|
+
* one accepting several shapes, is easy to emit as a bare
|
|
742
|
+
* `{ type: 'string' }` — and validating against them rejects calls the
|
|
743
|
+
* underlying API would have accepted. A Standard Schema ignores this flag.
|
|
744
|
+
*
|
|
745
|
+
* @default false
|
|
746
|
+
*/
|
|
747
|
+
validateArguments?: boolean;
|
|
748
|
+
/**
|
|
749
|
+
* The result's schema, advertised on `tools/list`. A result's
|
|
750
|
+
* `structuredContent` is validated against it.
|
|
751
|
+
*/
|
|
752
|
+
outputSchema?: ToolSchema;
|
|
753
|
+
/**
|
|
754
|
+
* Behaviour hints forwarded on `tools/list` (`readOnlyHint`,
|
|
755
|
+
* `destructiveHint`, `idempotentHint`, `openWorldHint`). Clients use them to
|
|
756
|
+
* decide when to ask the user for confirmation; they never enforce anything.
|
|
757
|
+
*/
|
|
758
|
+
annotations?: ToolAnnotations;
|
|
759
|
+
/** Groups the tool for the deferred `search` filter. Not sent on `tools/list`. */
|
|
760
|
+
tags?: string[];
|
|
761
|
+
/**
|
|
762
|
+
* The line the deferred `search` shows for the tool. Defaults to the first
|
|
763
|
+
* line of `description`. Not sent on `tools/list`.
|
|
764
|
+
*/
|
|
765
|
+
summary?: string;
|
|
766
|
+
/**
|
|
767
|
+
* Tool metadata forwarded verbatim on `tools/list`. The MCP Apps extension
|
|
768
|
+
* links a tool to its view through it: build the bag with
|
|
769
|
+
* `registerAppResource(...).toolMeta()`.
|
|
770
|
+
*/
|
|
771
|
+
_meta?: Record<string, unknown>;
|
|
772
|
+
/** Answers a call with the (validated, when enforced) arguments. */
|
|
773
|
+
handler: (args: Record<string, unknown>) => CallToolResult | Promise<CallToolResult>;
|
|
774
|
+
}
|
|
775
|
+
/** Parameters for {@link defineTool}. */
|
|
776
|
+
interface DefineToolParams extends Omit<Tool, 'handler'> {
|
|
777
|
+
/**
|
|
778
|
+
* Answers a call with plain data, which becomes the tool result: JSON text,
|
|
779
|
+
* plus `structuredContent` when `outputSchema` is set. Resolving to `null`
|
|
780
|
+
* or `undefined` answers `notFoundMessage` as an `isError` result.
|
|
781
|
+
*/
|
|
782
|
+
method: (args: Record<string, unknown>) => unknown;
|
|
783
|
+
/**
|
|
784
|
+
* The `isError` message for a `null` or `undefined` result.
|
|
785
|
+
* @default 'Not found'
|
|
786
|
+
*/
|
|
787
|
+
notFoundMessage?: string;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Builds a {@link Tool} from a `method` that returns data instead of a
|
|
791
|
+
* `CallToolResult`, which is what most hand-written tools want.
|
|
792
|
+
*
|
|
793
|
+
* @example
|
|
794
|
+
* ```typescript
|
|
795
|
+
* const getCampaign = defineTool({
|
|
796
|
+
* name: 'get-campaign',
|
|
797
|
+
* description: 'Get a campaign by id.',
|
|
798
|
+
* inputSchema: z.object({ campaignId: z.string() }),
|
|
799
|
+
* method: ({ campaignId }) => findCampaign(campaignId as string),
|
|
800
|
+
* notFoundMessage: 'Campaign not found',
|
|
801
|
+
* });
|
|
802
|
+
* ```
|
|
803
|
+
*/
|
|
804
|
+
declare const defineTool: ({
|
|
805
|
+
method,
|
|
806
|
+
notFoundMessage,
|
|
807
|
+
...tool
|
|
808
|
+
}: DefineToolParams) => Tool;
|
|
809
|
+
//#endregion
|
|
810
|
+
//#region src/toolSearch.d.ts
|
|
811
|
+
/** What the deferred `search` ranks: one call, already narrowed to the visible tools. */
|
|
812
|
+
interface ToolSearchArgs {
|
|
813
|
+
/** Free-text query. One without words lists the tools in order. */
|
|
814
|
+
query: string;
|
|
815
|
+
/** Only tools carrying this tag. */
|
|
816
|
+
tag?: string;
|
|
817
|
+
/** Maximum number of tools to return. */
|
|
818
|
+
limit: number;
|
|
819
|
+
/** The tools this request may see. */
|
|
820
|
+
tools: Tool[];
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* The deferred tools' default search: scores each tool by the query's terms,
|
|
824
|
+
* weighted name > summary > tags > description, and breaks ties by the
|
|
825
|
+
* tools' order. No index — a few hundred tools need none.
|
|
826
|
+
*/
|
|
827
|
+
declare const rankTools: ({
|
|
828
|
+
query,
|
|
829
|
+
tag,
|
|
830
|
+
limit,
|
|
831
|
+
tools
|
|
832
|
+
}: ToolSearchArgs) => Tool[];
|
|
833
|
+
//#endregion
|
|
834
|
+
//#region src/deferTools.d.ts
|
|
835
|
+
/** How {@link registerTools} defers tools behind `search` / `describe` / `call`. */
|
|
836
|
+
interface DeferToolsOptions {
|
|
837
|
+
/**
|
|
838
|
+
* Tools that are not deferred: they are also registered as standalone MCP
|
|
839
|
+
* tools, and stay reachable through `search` / `describe` / `call` too.
|
|
840
|
+
*
|
|
841
|
+
* Defaults to the tools linked to an MCP Apps view: a host finds a tool's
|
|
842
|
+
* view through that tool's own `tools/list` entry, so a view behind `call`
|
|
843
|
+
* would never render.
|
|
844
|
+
*/
|
|
845
|
+
except?: (args: {
|
|
846
|
+
tool: Tool;
|
|
847
|
+
}) => boolean;
|
|
848
|
+
/**
|
|
849
|
+
* The names of the three tools that reach the deferred ones, to avoid a
|
|
850
|
+
* collision with a standalone tool or to defer two sets on one server.
|
|
851
|
+
* @default { search: 'search', describe: 'describe', call: 'call' }
|
|
852
|
+
*/
|
|
853
|
+
names?: {
|
|
854
|
+
search?: string;
|
|
855
|
+
describe?: string;
|
|
856
|
+
call?: string;
|
|
857
|
+
};
|
|
858
|
+
/**
|
|
859
|
+
* Replaces the default ranking, e.g. with embeddings. Receives only the
|
|
860
|
+
* tools `visible` lets through; the result is cut to `limit`.
|
|
861
|
+
* {@link rankTools} is the default, for a fallback.
|
|
862
|
+
*/
|
|
863
|
+
search?: (args: ToolSearchArgs) => Tool[] | Promise<Tool[]>;
|
|
864
|
+
/**
|
|
865
|
+
* How many tools `search` returns when the model does not say.
|
|
866
|
+
* @default 10
|
|
867
|
+
*/
|
|
868
|
+
searchLimit?: number;
|
|
869
|
+
/**
|
|
870
|
+
* Runs on every `search`, `describe` and `call`. A tool it rejects is
|
|
871
|
+
* absent from all three; `call` answers it as an unknown name. Tools
|
|
872
|
+
* `except` keeps standalone are listed on `tools/list` regardless.
|
|
873
|
+
*
|
|
874
|
+
* @example
|
|
875
|
+
* ```typescript
|
|
876
|
+
* visible: ({ tool }) => canUse({ identity: getIdentity(), tool: tool.name }),
|
|
877
|
+
* ```
|
|
878
|
+
*/
|
|
879
|
+
visible?: (args: {
|
|
880
|
+
tool: Tool;
|
|
881
|
+
}) => boolean;
|
|
882
|
+
}
|
|
883
|
+
//#endregion
|
|
695
884
|
//#region src/registerAppResource.d.ts
|
|
696
885
|
/**
|
|
697
886
|
* Identifier of the MCP Apps extension (`io.modelcontextprotocol/ui`), used as
|
|
@@ -800,9 +989,8 @@ interface RegisteredAppResource {
|
|
|
800
989
|
/** The SDK's registration handle, for `update`/`disable`/`remove`. */
|
|
801
990
|
resource: RegisteredResource;
|
|
802
991
|
/**
|
|
803
|
-
* Builds the `_meta` bag that links a tool to this view. Pass it
|
|
804
|
-
* `
|
|
805
|
-
* whichever registration path the tool uses.
|
|
992
|
+
* Builds the `_meta` bag that links a tool to this view. Pass it as a
|
|
993
|
+
* `Tool`'s `_meta`, or to the SDK's `registerTool`.
|
|
806
994
|
*/
|
|
807
995
|
toolMeta: (params?: ToolMetaParams) => Record<string, unknown>;
|
|
808
996
|
}
|
|
@@ -883,113 +1071,59 @@ declare const registerAppResource: ({
|
|
|
883
1071
|
ui
|
|
884
1072
|
}: RegisterAppResourceParams) => RegisteredAppResource;
|
|
885
1073
|
//#endregion
|
|
886
|
-
//#region src/
|
|
887
|
-
/**
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
interface JsonObjectSchema {
|
|
894
|
-
type: 'object';
|
|
895
|
-
properties?: Record<string, unknown>;
|
|
896
|
-
required?: string[];
|
|
897
|
-
[key: string]: unknown;
|
|
898
|
-
}
|
|
899
|
-
/**
|
|
900
|
-
* Parameters accepted by {@link registerToolFromSchema}.
|
|
901
|
-
*/
|
|
902
|
-
interface RegisterToolFromSchemaParams {
|
|
903
|
-
/** Unique tool name. */
|
|
904
|
-
name: string;
|
|
905
|
-
/** Human-readable description shown to the AI client. */
|
|
906
|
-
description?: string;
|
|
907
|
-
/**
|
|
908
|
-
* Plain JSON Schema that describes the tool's input object.
|
|
909
|
-
* This schema is forwarded verbatim over the MCP wire protocol, so any
|
|
910
|
-
* JSON Schema feature (`anyOf`, `$ref`, `pattern`, …) is preserved without
|
|
911
|
-
* loss. Defaults to `{ type: 'object', properties: {} }` when omitted.
|
|
912
|
-
*/
|
|
913
|
-
inputSchema?: JsonObjectSchema;
|
|
1074
|
+
//#region src/registerTools.d.ts
|
|
1075
|
+
/** Parameters for {@link registerTools}. */
|
|
1076
|
+
interface RegisterToolsParams {
|
|
1077
|
+
/** The MCP server the tools are registered on. */
|
|
1078
|
+
server: McpServer$1;
|
|
1079
|
+
/** The tools to expose. */
|
|
1080
|
+
tools: Tool[];
|
|
914
1081
|
/**
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
*
|
|
918
|
-
* Defaults to `false`, which forwards arguments to the handler unchecked —
|
|
919
|
-
* the behavior this helper has always had. `inputSchema` is still advertised
|
|
920
|
-
* verbatim over `tools/list` either way; this only controls enforcement.
|
|
1082
|
+
* Defers `tools` behind three tools — `search`, `describe`, `call` —
|
|
1083
|
+
* instead of registering one MCP tool each. `true` takes every
|
|
1084
|
+
* {@link DeferToolsOptions} default.
|
|
921
1085
|
*
|
|
922
|
-
*
|
|
923
|
-
*
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
* the underlying API would have accepted.
|
|
1086
|
+
* A large tool set overflows a client's context: every definition ships to
|
|
1087
|
+
* the model on every turn, some providers cap the number of tools per
|
|
1088
|
+
* request, and clients that don't defer tool loading choke on the list.
|
|
1089
|
+
* Deferred, the model sees three small definitions, and a schema
|
|
1090
|
+
* enters its context only for the tools it is about to use.
|
|
928
1091
|
*
|
|
929
1092
|
* @default false
|
|
930
1093
|
*/
|
|
931
|
-
|
|
932
|
-
/**
|
|
933
|
-
* Tool metadata forwarded verbatim on `tools/list`.
|
|
934
|
-
*
|
|
935
|
-
* The MCP Apps extension links a tool to the view its result is rendered
|
|
936
|
-
* with through this field; build the bag with `toolMeta()` from
|
|
937
|
-
* `registerAppResource` rather than writing the keys by hand.
|
|
938
|
-
*
|
|
939
|
-
* @example
|
|
940
|
-
* ```typescript
|
|
941
|
-
* _meta: dashboard.toolMeta({ visibility: ['app'] }),
|
|
942
|
-
* ```
|
|
943
|
-
*/
|
|
944
|
-
_meta?: Record<string, unknown>;
|
|
945
|
-
/**
|
|
946
|
-
* Tool handler invoked when the AI client calls the tool.
|
|
947
|
-
* Receives the request arguments, validated against `inputSchema` only when
|
|
948
|
-
* `validateArguments` is enabled.
|
|
949
|
-
*/
|
|
950
|
-
handler: (args: Record<string, unknown>) => CallToolResult | Promise<CallToolResult>;
|
|
1094
|
+
defer?: boolean | DeferToolsOptions;
|
|
951
1095
|
}
|
|
952
1096
|
/**
|
|
953
|
-
* Registers
|
|
954
|
-
* `
|
|
955
|
-
*
|
|
956
|
-
* This is useful when tool definitions are shared between the MCP server and
|
|
957
|
-
* an AI SDK agent (e.g. Vercel AI SDK's `tool()` helper), because both consume
|
|
958
|
-
* a plain JSON Schema at runtime. Using this helper eliminates the lossy
|
|
959
|
-
* JSON-Schema→Zod conversion that would otherwise be required.
|
|
960
|
-
*
|
|
961
|
-
* Thin wrapper over `@modelcontextprotocol/server`'s `fromJsonSchema`, which
|
|
962
|
-
* converts a JSON Schema into a Standard Schema that `registerTool` accepts
|
|
963
|
-
* directly, so the schema round-trips verbatim over `tools/list`. Arguments
|
|
964
|
-
* reach the handler unchecked unless `validateArguments` is enabled.
|
|
965
|
-
*
|
|
966
|
-
* @param server - The `McpServer` instance to register the tool on.
|
|
967
|
-
* @param params - Tool configuration including name, description, inputSchema,
|
|
968
|
-
* validateArguments, `_meta`, and handler.
|
|
1097
|
+
* Registers tools on an MCP server, either one MCP tool each (the default)
|
|
1098
|
+
* or deferred behind `search` / `describe` / `call`. The same `tools` can feed
|
|
1099
|
+
* both, so two endpoints over one API cannot drift apart.
|
|
969
1100
|
*
|
|
970
1101
|
* @example
|
|
971
1102
|
* ```typescript
|
|
972
|
-
* import {
|
|
1103
|
+
* import { McpServer, registerTools, z } from '@ttoss/http-server-mcp';
|
|
973
1104
|
*
|
|
974
|
-
* const
|
|
975
|
-
*
|
|
976
|
-
*
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
*
|
|
980
|
-
*
|
|
981
|
-
*
|
|
982
|
-
* required: ['id'],
|
|
1105
|
+
* const tools = [
|
|
1106
|
+
* {
|
|
1107
|
+
* name: 'get-project',
|
|
1108
|
+
* description: 'Get a project by ID',
|
|
1109
|
+
* inputSchema: z.object({ id: z.string() }),
|
|
1110
|
+
* handler: async ({ id }) => ({
|
|
1111
|
+
* content: [{ type: 'text', text: `Project: ${id}` }],
|
|
1112
|
+
* }),
|
|
983
1113
|
* },
|
|
984
|
-
*
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
* });
|
|
1114
|
+
* ];
|
|
1115
|
+
*
|
|
1116
|
+
* registerTools({ server: fullServer, tools });
|
|
1117
|
+
* registerTools({ server: deferredServer, tools, defer: true });
|
|
988
1118
|
* ```
|
|
989
1119
|
*/
|
|
990
|
-
declare const
|
|
1120
|
+
declare const registerTools: ({
|
|
1121
|
+
server,
|
|
1122
|
+
tools,
|
|
1123
|
+
defer
|
|
1124
|
+
}: RegisterToolsParams) => void;
|
|
991
1125
|
//#endregion
|
|
992
|
-
//#region src/
|
|
1126
|
+
//#region src/toolGate.d.ts
|
|
993
1127
|
/** Resolved identity for a gated tool call. */
|
|
994
1128
|
type ToolIdentity = {
|
|
995
1129
|
userId: string;
|
|
@@ -1003,50 +1137,16 @@ type ToolIdentity = {
|
|
|
1003
1137
|
type ToolCallContext = {
|
|
1004
1138
|
/** The resolved caller identity. */identity: ToolIdentity;
|
|
1005
1139
|
/**
|
|
1006
|
-
* The validated tool input
|
|
1007
|
-
*
|
|
1008
|
-
* per call.
|
|
1140
|
+
* The validated tool input. Arg-conditional gates read this to vary their
|
|
1141
|
+
* predicate per call.
|
|
1009
1142
|
*/
|
|
1010
1143
|
args: Record<string, unknown>; /** Tool name, for error attribution and gate labelling. */
|
|
1011
1144
|
handler: string;
|
|
1012
1145
|
};
|
|
1013
|
-
/**
|
|
1014
|
-
type
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
title?: string;
|
|
1018
|
-
/**
|
|
1019
|
-
* Behaviour hints forwarded on `tools/list` (`readOnlyHint`,
|
|
1020
|
-
* `destructiveHint`, `idempotentHint`, `openWorldHint`). Clients use them to
|
|
1021
|
-
* decide when to ask the user for confirmation; they are hints, never
|
|
1022
|
-
* enforcement — scope and gates still decide what may run.
|
|
1023
|
-
*/
|
|
1024
|
-
annotations?: ToolAnnotations;
|
|
1025
|
-
/**
|
|
1026
|
-
* Zod field map or ZodObject describing the result. When set, a successful
|
|
1027
|
-
* result is also returned as `structuredContent`, and the SDK validates it
|
|
1028
|
-
* against this schema.
|
|
1029
|
-
*/
|
|
1030
|
-
outputSchema?: unknown; /** The single scope that must be present on the caller's token. */
|
|
1031
|
-
requiredScope: string; /** Zod field map or ZodObject — passed through to `server.registerTool`. */
|
|
1032
|
-
inputSchema: unknown;
|
|
1033
|
-
/**
|
|
1034
|
-
* Per-tool gates merged after the global `gates`. Useful for arg-conditional
|
|
1035
|
-
* authorization (e.g. different subscription tiers based on call args).
|
|
1036
|
-
* Each gate receives the full {@link ToolCallContext} (identity + args).
|
|
1037
|
-
* Throw to reject the call; return (or resolve) to continue.
|
|
1038
|
-
*/
|
|
1039
|
-
gates?: Array<(ctx: ToolCallContext) => void | Promise<void>>;
|
|
1040
|
-
/**
|
|
1041
|
-
* Tool metadata forwarded verbatim on `tools/list` — how a gated tool links
|
|
1042
|
-
* to an MCP Apps view (`registerAppResource(...).toolMeta()`).
|
|
1043
|
-
*/
|
|
1044
|
-
_meta?: Record<string, unknown>; /** The tool handler. Receives merged call args + `buildContext` output. */
|
|
1045
|
-
method: (args: Record<string, unknown>) => Promise<unknown>;
|
|
1046
|
-
};
|
|
1047
|
-
/** Options for {@link createGatedToolRegistrar}. */
|
|
1048
|
-
type CreateGatedToolRegistrarOptions = {
|
|
1049
|
-
/** The MCP server instance to register tools on. */server: McpServer$1;
|
|
1146
|
+
/** An authorization check. Throw to reject the call; return to continue. */
|
|
1147
|
+
type ToolCallGate = (ctx: ToolCallContext) => void | Promise<void>;
|
|
1148
|
+
/** Options for {@link createToolGate}. */
|
|
1149
|
+
type CreateToolGateOptions = {
|
|
1050
1150
|
/**
|
|
1051
1151
|
* Called once per tool invocation to resolve the caller's identity.
|
|
1052
1152
|
* Defaults to `getIdentity()` from the request context.
|
|
@@ -1057,12 +1157,11 @@ type CreateGatedToolRegistrarOptions = {
|
|
|
1057
1157
|
* per-tool gates. Each receives the full {@link ToolCallContext} — both
|
|
1058
1158
|
* identity and the validated call args — so gate predicates may be
|
|
1059
1159
|
* conditional on either.
|
|
1060
|
-
* Throw to reject the call; return (or resolve) to continue.
|
|
1061
1160
|
* Gates own their own error handling — `onError` covers the tool handler only.
|
|
1062
1161
|
*/
|
|
1063
|
-
gates?:
|
|
1162
|
+
gates?: ToolCallGate[];
|
|
1064
1163
|
/**
|
|
1065
|
-
* When `true` (default), checks `
|
|
1164
|
+
* When `true` (default), checks `requiredScope` against `identity.scopes`
|
|
1066
1165
|
* and returns an `isError` result when the scope is absent. Set to `false`
|
|
1067
1166
|
* to skip the scope check (e.g. when all scopes are enforced by `gates`).
|
|
1068
1167
|
*/
|
|
@@ -1079,11 +1178,6 @@ type CreateGatedToolRegistrarOptions = {
|
|
|
1079
1178
|
* so context can vary by identity or by call args.
|
|
1080
1179
|
*/
|
|
1081
1180
|
buildContext?: (ctx: ToolCallContext) => Record<string, unknown>;
|
|
1082
|
-
/**
|
|
1083
|
-
* Message returned as an `isError` result when the handler resolves to
|
|
1084
|
-
* `null` or `undefined`. Defaults to `"Not found"`.
|
|
1085
|
-
*/
|
|
1086
|
-
notFoundMessage?: string;
|
|
1087
1181
|
/**
|
|
1088
1182
|
* Renders a `LocalizedError` — thrown by a gate or by the handler — as an
|
|
1089
1183
|
* `isError` result `{ error: <message>, code }` in the caller's locale,
|
|
@@ -1101,18 +1195,79 @@ type CreateGatedToolRegistrarOptions = {
|
|
|
1101
1195
|
getLocale?: (ctx: ToolCallContext) => string | string[] | null | undefined | Promise<string | string[] | null | undefined>;
|
|
1102
1196
|
};
|
|
1103
1197
|
};
|
|
1104
|
-
|
|
1105
|
-
|
|
1198
|
+
/** Parameters for the gate {@link createToolGate} returns. */
|
|
1199
|
+
interface GateToolParams {
|
|
1200
|
+
/** The tool to protect. */
|
|
1201
|
+
tool: Tool;
|
|
1202
|
+
/** The single scope that must be present on the caller's token. */
|
|
1203
|
+
requiredScope: string;
|
|
1204
|
+
/**
|
|
1205
|
+
* Per-tool gates run after the global `gates`. Useful for arg-conditional
|
|
1206
|
+
* authorization (e.g. different subscription tiers based on call args).
|
|
1207
|
+
*/
|
|
1208
|
+
gates?: ToolCallGate[];
|
|
1209
|
+
}
|
|
1210
|
+
/** Wraps a {@link Tool}'s handler in the authorization pipeline. */
|
|
1211
|
+
type ToolGate = (params: GateToolParams) => Tool;
|
|
1212
|
+
/**
|
|
1213
|
+
* Factory for a gate: a function that wraps a {@link Tool} so every call
|
|
1214
|
+
* passes the same authentication and authorization pipeline before its
|
|
1215
|
+
* handler runs. The gated tool is an ordinary `Tool`, so it registers
|
|
1216
|
+
* directly or joins a catalog like any other.
|
|
1217
|
+
*
|
|
1218
|
+
* Every gated call:
|
|
1219
|
+
* 1. Resolves the caller identity (default: `getIdentity()`). Returns an
|
|
1220
|
+
* `isError` result when the identity is absent (unauthenticated request).
|
|
1221
|
+
* 2. When `enforceScope` is `true` (default), checks `requiredScope` — returns
|
|
1222
|
+
* an `isError` result when the scope is absent, consistent with how MCP
|
|
1223
|
+
* surfaces authorization errors.
|
|
1224
|
+
* 3. Runs global `gates` then the tool's `gates` in order; a throwing gate
|
|
1225
|
+
* rejects the call. Every gate receives the full {@link ToolCallContext}.
|
|
1226
|
+
* 4. Merges `buildContext` output into the handler args.
|
|
1227
|
+
* 5. Calls `onError` on handler throw before rethrowing.
|
|
1228
|
+
* 6. With `i18n`, renders a `LocalizedError` from a gate or the handler as an
|
|
1229
|
+
* `isError` result in the caller's locale instead of rethrowing it.
|
|
1230
|
+
*
|
|
1231
|
+
* @example
|
|
1232
|
+
* ```typescript
|
|
1233
|
+
* const gate = createToolGate({
|
|
1234
|
+
* resolveIdentity: () => {
|
|
1235
|
+
* const jwt = getIdentity<{ sub: string; scopes: string[] }>();
|
|
1236
|
+
* return { userId: jwt!.sub, scopes: jwt!.scopes };
|
|
1237
|
+
* },
|
|
1238
|
+
* });
|
|
1239
|
+
*
|
|
1240
|
+
* registerTools({
|
|
1241
|
+
* server,
|
|
1242
|
+
* tools: [
|
|
1243
|
+
* gate({
|
|
1244
|
+
* requiredScope: 'campaigns:write',
|
|
1245
|
+
* // arg-conditional gate: active vs. inactive have different restrictions
|
|
1246
|
+
* gates: [
|
|
1247
|
+
* ({ args }) =>
|
|
1248
|
+
* args.isActive
|
|
1249
|
+
* ? subscriptionGate(['mustNotExceedMaxActive', 'mustHaveBudget'])
|
|
1250
|
+
* : subscriptionGate(['mustIncludeService']),
|
|
1251
|
+
* ],
|
|
1252
|
+
* tool: defineTool({
|
|
1253
|
+
* name: 'activate-campaign',
|
|
1254
|
+
* description: 'Activate or deactivate a campaign.',
|
|
1255
|
+
* inputSchema: z.object({ isActive: z.boolean() }),
|
|
1256
|
+
* method: ({ userId, isActive }) => toggleCampaign(userId, isActive),
|
|
1257
|
+
* }),
|
|
1258
|
+
* }),
|
|
1259
|
+
* ],
|
|
1260
|
+
* });
|
|
1261
|
+
* ```
|
|
1262
|
+
*/
|
|
1263
|
+
declare const createToolGate: ({
|
|
1106
1264
|
resolveIdentity,
|
|
1107
1265
|
gates,
|
|
1108
1266
|
enforceScope,
|
|
1109
1267
|
onError,
|
|
1110
1268
|
buildContext,
|
|
1111
|
-
notFoundMessage,
|
|
1112
1269
|
i18n
|
|
1113
|
-
}
|
|
1114
|
-
register: (def: GatedToolDef) => void;
|
|
1115
|
-
};
|
|
1270
|
+
}?: CreateToolGateOptions) => ToolGate;
|
|
1116
1271
|
//#endregion
|
|
1117
1272
|
//#region src/index.d.ts
|
|
1118
1273
|
type Context$1 = Application.Context;
|
|
@@ -1551,4 +1706,4 @@ interface McpRouterOptions {
|
|
|
1551
1706
|
*/
|
|
1552
1707
|
declare const createMcpRouter: (server: McpServer$1, options?: McpRouterOptions) => McpRouter;
|
|
1553
1708
|
//#endregion
|
|
1554
|
-
export { ApiCallOptions, CognitoUserPoolConfig, type
|
|
1709
|
+
export { ApiCallOptions, CognitoUserPoolConfig, type CreateToolGateOptions, type DeferToolsOptions, type DefineToolParams, type GateToolParams, InMemoryServerEventBus, type JsonObjectSchema, McpAuthOptions, McpRouter, McpRouterOptions, McpServer, type RegisterAppResourceParams, type RegisterToolsParams, type RegisteredAppResource, type ServerEvent, type ServerEventBus, type ServerNotifier, type Tool, type ToolCallContext, type ToolCallGate, type ToolGate, type ToolIdentity, type ToolMetaParams, type ToolSchema, type ToolSearchArgs, UI_EXTENSION_ID, UI_RESOURCE_MIME_TYPE, type UiResourceCsp, type UiResourceMeta, type UiResourcePermissions, type UiToolVisibility, apiCall, checkScopes, createMcpRouter, createToolGate, defineTool, errorBodyMessage, getApiHeaders, getIdentity, getRequestLocale, rankTools, registerAppResource, registerTools, z };
|