@vinkius-core/mcp-fusion 0.1.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 +190 -0
- package/README.md +391 -0
- package/dist/src/AbstractBase.d.ts +24 -0
- package/dist/src/AbstractBase.d.ts.map +1 -0
- package/dist/src/AbstractBase.js +63 -0
- package/dist/src/AbstractBase.js.map +1 -0
- package/dist/src/AbstractLeaf.d.ts +12 -0
- package/dist/src/AbstractLeaf.d.ts.map +1 -0
- package/dist/src/AbstractLeaf.js +32 -0
- package/dist/src/AbstractLeaf.js.map +1 -0
- package/dist/src/Annotations.d.ts +15 -0
- package/dist/src/Annotations.d.ts.map +1 -0
- package/dist/src/Annotations.js +29 -0
- package/dist/src/Annotations.js.map +1 -0
- package/dist/src/Group.d.ts +32 -0
- package/dist/src/Group.d.ts.map +1 -0
- package/dist/src/Group.js +131 -0
- package/dist/src/Group.js.map +1 -0
- package/dist/src/Icon.d.ts +19 -0
- package/dist/src/Icon.d.ts.map +1 -0
- package/dist/src/Icon.js +33 -0
- package/dist/src/Icon.js.map +1 -0
- package/dist/src/Prompt.d.ts +11 -0
- package/dist/src/Prompt.d.ts.map +1 -0
- package/dist/src/Prompt.js +28 -0
- package/dist/src/Prompt.js.map +1 -0
- package/dist/src/PromptArgument.d.ts +10 -0
- package/dist/src/PromptArgument.d.ts.map +1 -0
- package/dist/src/PromptArgument.js +20 -0
- package/dist/src/PromptArgument.js.map +1 -0
- package/dist/src/Resource.d.ts +19 -0
- package/dist/src/Resource.d.ts.map +1 -0
- package/dist/src/Resource.js +34 -0
- package/dist/src/Resource.js.map +1 -0
- package/dist/src/Role.d.ts +5 -0
- package/dist/src/Role.d.ts.map +1 -0
- package/dist/src/Role.js +6 -0
- package/dist/src/Role.js.map +1 -0
- package/dist/src/Tool.d.ts +16 -0
- package/dist/src/Tool.d.ts.map +1 -0
- package/dist/src/Tool.js +28 -0
- package/dist/src/Tool.js.map +1 -0
- package/dist/src/ToolAnnotations.d.ts +23 -0
- package/dist/src/ToolAnnotations.d.ts.map +1 -0
- package/dist/src/ToolAnnotations.js +44 -0
- package/dist/src/ToolAnnotations.js.map +1 -0
- package/dist/src/converters/GroupConverter.d.ts +14 -0
- package/dist/src/converters/GroupConverter.d.ts.map +1 -0
- package/dist/src/converters/GroupConverter.js +13 -0
- package/dist/src/converters/GroupConverter.js.map +1 -0
- package/dist/src/converters/PromptConverter.d.ts +14 -0
- package/dist/src/converters/PromptConverter.d.ts.map +1 -0
- package/dist/src/converters/PromptConverter.js +13 -0
- package/dist/src/converters/PromptConverter.js.map +1 -0
- package/dist/src/converters/ResourceConverter.d.ts +14 -0
- package/dist/src/converters/ResourceConverter.d.ts.map +1 -0
- package/dist/src/converters/ResourceConverter.js +13 -0
- package/dist/src/converters/ResourceConverter.js.map +1 -0
- package/dist/src/converters/ToolAnnotationsConverter.d.ts +16 -0
- package/dist/src/converters/ToolAnnotationsConverter.d.ts.map +1 -0
- package/dist/src/converters/ToolAnnotationsConverter.js +23 -0
- package/dist/src/converters/ToolAnnotationsConverter.js.map +1 -0
- package/dist/src/converters/ToolConverter.d.ts +14 -0
- package/dist/src/converters/ToolConverter.d.ts.map +1 -0
- package/dist/src/converters/ToolConverter.js +13 -0
- package/dist/src/converters/ToolConverter.js.map +1 -0
- package/dist/src/converters/index.d.ts +6 -0
- package/dist/src/converters/index.d.ts.map +1 -0
- package/dist/src/converters/index.js +6 -0
- package/dist/src/converters/index.js.map +1 -0
- package/dist/src/framework/GroupedToolBuilder.d.ts +137 -0
- package/dist/src/framework/GroupedToolBuilder.d.ts.map +1 -0
- package/dist/src/framework/GroupedToolBuilder.js +289 -0
- package/dist/src/framework/GroupedToolBuilder.js.map +1 -0
- package/dist/src/framework/ResponseHelper.d.ts +43 -0
- package/dist/src/framework/ResponseHelper.d.ts.map +1 -0
- package/dist/src/framework/ResponseHelper.js +49 -0
- package/dist/src/framework/ResponseHelper.js.map +1 -0
- package/dist/src/framework/ToolBuilder.d.ts +46 -0
- package/dist/src/framework/ToolBuilder.d.ts.map +1 -0
- package/dist/src/framework/ToolBuilder.js +2 -0
- package/dist/src/framework/ToolBuilder.js.map +1 -0
- package/dist/src/framework/ToolRegistry.d.ts +85 -0
- package/dist/src/framework/ToolRegistry.d.ts.map +1 -0
- package/dist/src/framework/ToolRegistry.js +153 -0
- package/dist/src/framework/ToolRegistry.js.map +1 -0
- package/dist/src/framework/index.d.ts +9 -0
- package/dist/src/framework/index.d.ts.map +1 -0
- package/dist/src/framework/index.js +8 -0
- package/dist/src/framework/index.js.map +1 -0
- package/dist/src/framework/strategies/AnnotationAggregator.d.ts +11 -0
- package/dist/src/framework/strategies/AnnotationAggregator.d.ts.map +1 -0
- package/dist/src/framework/strategies/AnnotationAggregator.js +25 -0
- package/dist/src/framework/strategies/AnnotationAggregator.js.map +1 -0
- package/dist/src/framework/strategies/DescriptionGenerator.d.ts +12 -0
- package/dist/src/framework/strategies/DescriptionGenerator.d.ts.map +1 -0
- package/dist/src/framework/strategies/DescriptionGenerator.js +70 -0
- package/dist/src/framework/strategies/DescriptionGenerator.js.map +1 -0
- package/dist/src/framework/strategies/MiddlewareCompiler.d.ts +13 -0
- package/dist/src/framework/strategies/MiddlewareCompiler.d.ts.map +1 -0
- package/dist/src/framework/strategies/MiddlewareCompiler.js +24 -0
- package/dist/src/framework/strategies/MiddlewareCompiler.js.map +1 -0
- package/dist/src/framework/strategies/SchemaGenerator.d.ts +15 -0
- package/dist/src/framework/strategies/SchemaGenerator.d.ts.map +1 -0
- package/dist/src/framework/strategies/SchemaGenerator.js +97 -0
- package/dist/src/framework/strategies/SchemaGenerator.js.map +1 -0
- package/dist/src/framework/strategies/SchemaUtils.d.ts +7 -0
- package/dist/src/framework/strategies/SchemaUtils.d.ts.map +1 -0
- package/dist/src/framework/strategies/SchemaUtils.js +17 -0
- package/dist/src/framework/strategies/SchemaUtils.js.map +1 -0
- package/dist/src/framework/strategies/ToonDescriptionGenerator.d.ts +3 -0
- package/dist/src/framework/strategies/ToonDescriptionGenerator.d.ts.map +1 -0
- package/dist/src/framework/strategies/ToonDescriptionGenerator.js +53 -0
- package/dist/src/framework/strategies/ToonDescriptionGenerator.js.map +1 -0
- package/dist/src/framework/strategies/Types.d.ts +34 -0
- package/dist/src/framework/strategies/Types.d.ts.map +1 -0
- package/dist/src/framework/strategies/Types.js +2 -0
- package/dist/src/framework/strategies/Types.js.map +1 -0
- package/dist/src/framework/strategies/index.d.ts +12 -0
- package/dist/src/framework/strategies/index.d.ts.map +1 -0
- package/dist/src/framework/strategies/index.js +11 -0
- package/dist/src/framework/strategies/index.js.map +1 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +15 -0
- package/dist/src/index.js.map +1 -0
- package/dist/tests/AbstractBase.test.d.ts +2 -0
- package/dist/tests/AbstractBase.test.d.ts.map +1 -0
- package/dist/tests/AbstractBase.test.js +130 -0
- package/dist/tests/AbstractBase.test.js.map +1 -0
- package/dist/tests/AbstractLeaf.test.d.ts +2 -0
- package/dist/tests/AbstractLeaf.test.d.ts.map +1 -0
- package/dist/tests/AbstractLeaf.test.js +65 -0
- package/dist/tests/AbstractLeaf.test.js.map +1 -0
- package/dist/tests/Annotations.test.d.ts +2 -0
- package/dist/tests/Annotations.test.d.ts.map +1 -0
- package/dist/tests/Annotations.test.js +34 -0
- package/dist/tests/Annotations.test.js.map +1 -0
- package/dist/tests/BarrelExport.test.d.ts +2 -0
- package/dist/tests/BarrelExport.test.d.ts.map +1 -0
- package/dist/tests/BarrelExport.test.js +42 -0
- package/dist/tests/BarrelExport.test.js.map +1 -0
- package/dist/tests/Converters.test.d.ts +2 -0
- package/dist/tests/Converters.test.d.ts.map +1 -0
- package/dist/tests/Converters.test.js +193 -0
- package/dist/tests/Converters.test.js.map +1 -0
- package/dist/tests/Group.test.d.ts +2 -0
- package/dist/tests/Group.test.d.ts.map +1 -0
- package/dist/tests/Group.test.js +257 -0
- package/dist/tests/Group.test.js.map +1 -0
- package/dist/tests/Icon.test.d.ts +2 -0
- package/dist/tests/Icon.test.d.ts.map +1 -0
- package/dist/tests/Icon.test.js +44 -0
- package/dist/tests/Icon.test.js.map +1 -0
- package/dist/tests/Prompt.test.d.ts +2 -0
- package/dist/tests/Prompt.test.d.ts.map +1 -0
- package/dist/tests/Prompt.test.js +63 -0
- package/dist/tests/Prompt.test.js.map +1 -0
- package/dist/tests/PromptArgument.test.d.ts +2 -0
- package/dist/tests/PromptArgument.test.d.ts.map +1 -0
- package/dist/tests/PromptArgument.test.js +37 -0
- package/dist/tests/PromptArgument.test.js.map +1 -0
- package/dist/tests/Resource.test.d.ts +2 -0
- package/dist/tests/Resource.test.d.ts.map +1 -0
- package/dist/tests/Resource.test.js +61 -0
- package/dist/tests/Resource.test.js.map +1 -0
- package/dist/tests/Role.test.d.ts +2 -0
- package/dist/tests/Role.test.d.ts.map +1 -0
- package/dist/tests/Role.test.js +17 -0
- package/dist/tests/Role.test.js.map +1 -0
- package/dist/tests/Tool.test.d.ts +2 -0
- package/dist/tests/Tool.test.d.ts.map +1 -0
- package/dist/tests/Tool.test.js +62 -0
- package/dist/tests/Tool.test.js.map +1 -0
- package/dist/tests/ToolAnnotations.test.d.ts +2 -0
- package/dist/tests/ToolAnnotations.test.d.ts.map +1 -0
- package/dist/tests/ToolAnnotations.test.js +55 -0
- package/dist/tests/ToolAnnotations.test.js.map +1 -0
- package/dist/tests/framework/AdversarialQA.test.d.ts +2 -0
- package/dist/tests/framework/AdversarialQA.test.d.ts.map +1 -0
- package/dist/tests/framework/AdversarialQA.test.js +906 -0
- package/dist/tests/framework/AdversarialQA.test.js.map +1 -0
- package/dist/tests/framework/GroupedToolBuilder.test.d.ts +2 -0
- package/dist/tests/framework/GroupedToolBuilder.test.d.ts.map +1 -0
- package/dist/tests/framework/GroupedToolBuilder.test.js +712 -0
- package/dist/tests/framework/GroupedToolBuilder.test.js.map +1 -0
- package/dist/tests/framework/LargeScaleScenarios.test.d.ts +2 -0
- package/dist/tests/framework/LargeScaleScenarios.test.d.ts.map +1 -0
- package/dist/tests/framework/LargeScaleScenarios.test.js +1043 -0
- package/dist/tests/framework/LargeScaleScenarios.test.js.map +1 -0
- package/dist/tests/framework/McpServerAdapter.test.d.ts +2 -0
- package/dist/tests/framework/McpServerAdapter.test.d.ts.map +1 -0
- package/dist/tests/framework/McpServerAdapter.test.js +380 -0
- package/dist/tests/framework/McpServerAdapter.test.js.map +1 -0
- package/dist/tests/framework/ResponseHelper.test.d.ts +2 -0
- package/dist/tests/framework/ResponseHelper.test.d.ts.map +1 -0
- package/dist/tests/framework/ResponseHelper.test.js +202 -0
- package/dist/tests/framework/ResponseHelper.test.js.map +1 -0
- package/dist/tests/framework/SecurityDeep.test.d.ts +2 -0
- package/dist/tests/framework/SecurityDeep.test.d.ts.map +1 -0
- package/dist/tests/framework/SecurityDeep.test.js +825 -0
- package/dist/tests/framework/SecurityDeep.test.js.map +1 -0
- package/dist/tests/framework/ToolRegistry.test.d.ts +2 -0
- package/dist/tests/framework/ToolRegistry.test.d.ts.map +1 -0
- package/dist/tests/framework/ToolRegistry.test.js +152 -0
- package/dist/tests/framework/ToolRegistry.test.js.map +1 -0
- package/dist/tests/framework/ToonDescription.test.d.ts +2 -0
- package/dist/tests/framework/ToonDescription.test.d.ts.map +1 -0
- package/dist/tests/framework/ToonDescription.test.js +287 -0
- package/dist/tests/framework/ToonDescription.test.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { error } from './ResponseHelper.js';
|
|
2
|
+
import { generateDescription } from './strategies/DescriptionGenerator.js';
|
|
3
|
+
import { generateToonDescription } from './strategies/ToonDescriptionGenerator.js';
|
|
4
|
+
import { generateInputSchema } from './strategies/SchemaGenerator.js';
|
|
5
|
+
import { aggregateAnnotations } from './strategies/AnnotationAggregator.js';
|
|
6
|
+
import { compileMiddlewareChains } from './strategies/MiddlewareCompiler.js';
|
|
7
|
+
import { getActionRequiredFields } from './strategies/SchemaUtils.js';
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// ActionGroupBuilder (used in .group() callback)
|
|
10
|
+
// ============================================================================
|
|
11
|
+
export class ActionGroupBuilder {
|
|
12
|
+
constructor(groupName, description) {
|
|
13
|
+
/** @internal */
|
|
14
|
+
this._actions = [];
|
|
15
|
+
this._groupMiddlewares = [];
|
|
16
|
+
this._groupName = groupName;
|
|
17
|
+
this._groupDescription = description || '';
|
|
18
|
+
}
|
|
19
|
+
/** Add middleware scoped to this group */
|
|
20
|
+
use(mw) {
|
|
21
|
+
this._groupMiddlewares.push(mw);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
action(config) {
|
|
25
|
+
if (config.name.includes('.')) {
|
|
26
|
+
throw new Error(`Action name "${config.name}" must not contain dots. ` +
|
|
27
|
+
`The framework uses dots internally for group.action compound keys.`);
|
|
28
|
+
}
|
|
29
|
+
this._actions.push({
|
|
30
|
+
key: `${this._groupName}.${config.name}`,
|
|
31
|
+
groupName: this._groupName,
|
|
32
|
+
groupDescription: this._groupDescription,
|
|
33
|
+
actionName: config.name,
|
|
34
|
+
description: config.description,
|
|
35
|
+
schema: config.schema,
|
|
36
|
+
destructive: config.destructive,
|
|
37
|
+
idempotent: config.idempotent,
|
|
38
|
+
readOnly: config.readOnly,
|
|
39
|
+
handler: config.handler,
|
|
40
|
+
middlewares: this._groupMiddlewares.length > 0
|
|
41
|
+
? [...this._groupMiddlewares] : undefined,
|
|
42
|
+
});
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// GroupedToolBuilder
|
|
48
|
+
// ============================================================================
|
|
49
|
+
export class GroupedToolBuilder {
|
|
50
|
+
constructor(name) {
|
|
51
|
+
this._discriminator = 'action';
|
|
52
|
+
this._tags = [];
|
|
53
|
+
this._middlewares = [];
|
|
54
|
+
this._actions = [];
|
|
55
|
+
this._hasFlat = false;
|
|
56
|
+
this._hasGroup = false;
|
|
57
|
+
this._toonMode = false;
|
|
58
|
+
this._frozen = false;
|
|
59
|
+
this._name = name;
|
|
60
|
+
}
|
|
61
|
+
// ── Configuration (fluent) ──────────────────────────
|
|
62
|
+
/** Set the discriminator field name (default: "action") */
|
|
63
|
+
discriminator(field) {
|
|
64
|
+
this._assertNotFrozen();
|
|
65
|
+
this._discriminator = field;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/** Set the tool description (first line) */
|
|
69
|
+
description(desc) {
|
|
70
|
+
this._assertNotFrozen();
|
|
71
|
+
this._description = desc;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
/** Set MCP tool annotations */
|
|
75
|
+
annotations(a) {
|
|
76
|
+
this._assertNotFrozen();
|
|
77
|
+
this._annotations = a;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/** Set capability tags for selective exposure */
|
|
81
|
+
tags(...tags) {
|
|
82
|
+
this._assertNotFrozen();
|
|
83
|
+
this._tags = tags;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
/** Set common schema shared by all actions (propagates types to handlers) */
|
|
87
|
+
commonSchema(schema) {
|
|
88
|
+
this._assertNotFrozen();
|
|
89
|
+
this._commonSchema = schema;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Enable TOON-formatted descriptions for token optimization.
|
|
94
|
+
*
|
|
95
|
+
* Uses TOON (Token-Oriented Object Notation) to encode action metadata
|
|
96
|
+
* in a compact tabular format, reducing description token count by ~30-50%.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* new GroupedToolBuilder('projects')
|
|
101
|
+
* .description('Manage projects')
|
|
102
|
+
* .toonDescription()
|
|
103
|
+
* .action({ name: 'list', ... })
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
toonDescription() {
|
|
107
|
+
this._assertNotFrozen();
|
|
108
|
+
this._toonMode = true;
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/** Add middleware to the chain */
|
|
112
|
+
use(mw) {
|
|
113
|
+
this._assertNotFrozen();
|
|
114
|
+
this._middlewares.push(mw);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
action(config) {
|
|
118
|
+
this._assertNotFrozen();
|
|
119
|
+
if (this._hasGroup) {
|
|
120
|
+
throw new Error(`Cannot use .action() and .group() on the same builder "${this._name}". ` +
|
|
121
|
+
`Use .action() for flat tools OR .group() for hierarchical tools.`);
|
|
122
|
+
}
|
|
123
|
+
this._hasFlat = true;
|
|
124
|
+
if (config.name.includes('.')) {
|
|
125
|
+
throw new Error(`Action name "${config.name}" must not contain dots. ` +
|
|
126
|
+
`The framework uses dots internally for group.action compound keys.`);
|
|
127
|
+
}
|
|
128
|
+
this._actions.push({
|
|
129
|
+
key: config.name,
|
|
130
|
+
actionName: config.name,
|
|
131
|
+
description: config.description,
|
|
132
|
+
schema: config.schema,
|
|
133
|
+
destructive: config.destructive,
|
|
134
|
+
idempotent: config.idempotent,
|
|
135
|
+
readOnly: config.readOnly,
|
|
136
|
+
handler: config.handler,
|
|
137
|
+
});
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
group(name, descriptionOrConfigure, maybeConfigure) {
|
|
141
|
+
this._assertNotFrozen();
|
|
142
|
+
const description = typeof descriptionOrConfigure === 'string'
|
|
143
|
+
? descriptionOrConfigure
|
|
144
|
+
: undefined;
|
|
145
|
+
const configure = typeof descriptionOrConfigure === 'function'
|
|
146
|
+
? descriptionOrConfigure
|
|
147
|
+
: maybeConfigure;
|
|
148
|
+
if (this._hasFlat) {
|
|
149
|
+
throw new Error(`Cannot use .group() and .action() on the same builder "${this._name}". ` +
|
|
150
|
+
`Use .action() for flat tools OR .group() for hierarchical tools.`);
|
|
151
|
+
}
|
|
152
|
+
if (name.includes('.')) {
|
|
153
|
+
throw new Error(`Group name "${name}" must not contain dots.`);
|
|
154
|
+
}
|
|
155
|
+
this._hasGroup = true;
|
|
156
|
+
const groupBuilder = new ActionGroupBuilder(name, description);
|
|
157
|
+
configure(groupBuilder);
|
|
158
|
+
this._actions.push(...groupBuilder._actions);
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
// ── Build ───────────────────────────────────────────
|
|
162
|
+
/** Generate the MCP Tool definition. Caches result and freezes the builder. */
|
|
163
|
+
buildToolDefinition() {
|
|
164
|
+
if (this._cachedTool)
|
|
165
|
+
return this._cachedTool;
|
|
166
|
+
if (this._actions.length === 0) {
|
|
167
|
+
throw new Error(`Builder "${this._name}" has no actions registered.`);
|
|
168
|
+
}
|
|
169
|
+
// Delegate to strategy functions
|
|
170
|
+
const descriptionFn = this._toonMode ? generateToonDescription : generateDescription;
|
|
171
|
+
const description = descriptionFn(this._actions, this._name, this._description, this._hasGroup);
|
|
172
|
+
const inputSchema = generateInputSchema(this._actions, this._discriminator, this._hasGroup, this._commonSchema);
|
|
173
|
+
const annotations = aggregateAnnotations(this._actions, this._annotations);
|
|
174
|
+
const tool = {
|
|
175
|
+
name: this._name,
|
|
176
|
+
description,
|
|
177
|
+
inputSchema,
|
|
178
|
+
};
|
|
179
|
+
if (annotations && Object.keys(annotations).length > 0) {
|
|
180
|
+
tool.annotations = annotations;
|
|
181
|
+
}
|
|
182
|
+
// Pre-compile middleware chains via strategy
|
|
183
|
+
this._compiledChain = compileMiddlewareChains(this._actions, this._middlewares);
|
|
184
|
+
// Cache, freeze builder, and seal actions array
|
|
185
|
+
this._cachedTool = tool;
|
|
186
|
+
this._frozen = true;
|
|
187
|
+
Object.freeze(this._actions);
|
|
188
|
+
return tool;
|
|
189
|
+
}
|
|
190
|
+
// ── Execute ─────────────────────────────────────────
|
|
191
|
+
/** Route a call: validate → middleware → handler */
|
|
192
|
+
async execute(ctx, args) {
|
|
193
|
+
// Ensure built
|
|
194
|
+
if (!this._compiledChain) {
|
|
195
|
+
this.buildToolDefinition();
|
|
196
|
+
}
|
|
197
|
+
// 1. Parse discriminator
|
|
198
|
+
const discriminatorValue = args[this._discriminator];
|
|
199
|
+
if (!discriminatorValue) {
|
|
200
|
+
return error(`Error: ${this._discriminator} is required. ` +
|
|
201
|
+
`Available: ${this._actions.map(a => a.key).join(', ')}`);
|
|
202
|
+
}
|
|
203
|
+
// 2. Find action
|
|
204
|
+
const action = this._actions.find(a => a.key === discriminatorValue);
|
|
205
|
+
if (!action) {
|
|
206
|
+
return error(`Error: Unknown ${this._discriminator} "${discriminatorValue}". ` +
|
|
207
|
+
`Available: ${this._actions.map(a => a.key).join(', ')}`);
|
|
208
|
+
}
|
|
209
|
+
// 3. Validate & strip args with Zod
|
|
210
|
+
const validationSchema = this._buildValidationSchema(action);
|
|
211
|
+
if (validationSchema) {
|
|
212
|
+
// Remove discriminator before validation
|
|
213
|
+
const { [this._discriminator]: _, ...argsWithoutDiscriminator } = args;
|
|
214
|
+
const result = validationSchema.safeParse(argsWithoutDiscriminator);
|
|
215
|
+
if (!result.success) {
|
|
216
|
+
const issues = result.error.issues
|
|
217
|
+
.map(i => `${i.path.join('.')}: ${i.message}`)
|
|
218
|
+
.join('; ');
|
|
219
|
+
return error(`Validation failed: ${issues}`);
|
|
220
|
+
}
|
|
221
|
+
// Use validated + stripped args
|
|
222
|
+
args = { ...result.data, [this._discriminator]: discriminatorValue };
|
|
223
|
+
}
|
|
224
|
+
// 4. Run pre-compiled middleware chain → handler
|
|
225
|
+
const chain = this._compiledChain.get(action.key);
|
|
226
|
+
try {
|
|
227
|
+
return await chain(ctx, args);
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
231
|
+
return error(`[${this._name}/${discriminatorValue}] ${message}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// ── Introspection ───────────────────────────────────
|
|
235
|
+
/** Get the tool name */
|
|
236
|
+
getName() {
|
|
237
|
+
return this._name;
|
|
238
|
+
}
|
|
239
|
+
/** Get the tags */
|
|
240
|
+
getTags() {
|
|
241
|
+
return [...this._tags];
|
|
242
|
+
}
|
|
243
|
+
/** Get all action keys */
|
|
244
|
+
getActionNames() {
|
|
245
|
+
return this._actions.map(a => a.key);
|
|
246
|
+
}
|
|
247
|
+
/** Get metadata for all registered actions (for enterprise observability) */
|
|
248
|
+
getActionMetadata() {
|
|
249
|
+
return this._actions.map(a => {
|
|
250
|
+
var _a, _b, _c, _d, _e;
|
|
251
|
+
return ({
|
|
252
|
+
key: a.key,
|
|
253
|
+
actionName: a.actionName,
|
|
254
|
+
groupName: a.groupName,
|
|
255
|
+
description: a.description,
|
|
256
|
+
destructive: (_a = a.destructive) !== null && _a !== void 0 ? _a : false,
|
|
257
|
+
idempotent: (_b = a.idempotent) !== null && _b !== void 0 ? _b : false,
|
|
258
|
+
readOnly: (_c = a.readOnly) !== null && _c !== void 0 ? _c : false,
|
|
259
|
+
requiredFields: getActionRequiredFields(a),
|
|
260
|
+
hasMiddleware: ((_e = (_d = a.middlewares) === null || _d === void 0 ? void 0 : _d.length) !== null && _e !== void 0 ? _e : 0) > 0,
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
// ── Private: Validation Schema Building ─────────────
|
|
265
|
+
_buildValidationSchema(action) {
|
|
266
|
+
if (!this._commonSchema && !action.schema)
|
|
267
|
+
return null;
|
|
268
|
+
if (this._commonSchema && action.schema) {
|
|
269
|
+
return this._commonSchema.merge(action.schema).strip();
|
|
270
|
+
}
|
|
271
|
+
if (this._commonSchema) {
|
|
272
|
+
return this._commonSchema.strip();
|
|
273
|
+
}
|
|
274
|
+
if (action.schema) {
|
|
275
|
+
return action.schema.strip();
|
|
276
|
+
}
|
|
277
|
+
// Unreachable: all combinations covered above
|
|
278
|
+
/* istanbul ignore next */
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
// ── Private: Guards ─────────────────────────────────
|
|
282
|
+
_assertNotFrozen() {
|
|
283
|
+
if (this._frozen) {
|
|
284
|
+
throw new Error(`Builder "${this._name}" is frozen after buildToolDefinition(). ` +
|
|
285
|
+
`Cannot modify a built tool.`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
//# sourceMappingURL=GroupedToolBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GroupedToolBuilder.js","sourceRoot":"","sources":["../../../src/framework/GroupedToolBuilder.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAqB,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAC5E,OAAO,EAAE,uBAAuB,EAAsB,MAAM,oCAAoC,CAAC;AAEjG,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AA8BtE,+EAA+E;AAC/E,iDAAiD;AACjD,+EAA+E;AAE/E,MAAM,OAAO,kBAAkB;IAO3B,YAAY,SAAiB,EAAE,WAAoB;QANnD,gBAAgB;QACP,aAAQ,GAA+B,EAAE,CAAC;QAGlC,sBAAiB,GAA6B,EAAE,CAAC;QAG9D,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,iBAAiB,GAAG,WAAW,IAAI,EAAE,CAAC;IAC/C,CAAC;IAED,0CAA0C;IAC1C,GAAG,CAAC,EAA0B;QAC1B,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC;IAcD,MAAM,CAAC,MAA8B;QACjC,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CACX,gBAAgB,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,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC;QAC3D,CAAC;QACD,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;QACtC,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAChB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACjC,CAAC;QAED,8CAA8C;QAC9C,0BAA0B;QAC1B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,uDAAuD;IAE/C,gBAAgB;QACpB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CACX,YAAY,IAAI,CAAC,KAAK,2CAA2C;gBACjE,6BAA6B,CAChC,CAAC;QACN,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response Helpers
|
|
3
|
+
*
|
|
4
|
+
* Universal MCP response builders. No API coupling.
|
|
5
|
+
* These produce the standard MCP ToolResponse format.
|
|
6
|
+
*/
|
|
7
|
+
import { type EncodeOptions } from '@toon-format/toon';
|
|
8
|
+
/** Standard MCP tool response */
|
|
9
|
+
export interface ToolResponse {
|
|
10
|
+
content: Array<{
|
|
11
|
+
type: "text";
|
|
12
|
+
text: string;
|
|
13
|
+
}>;
|
|
14
|
+
isError?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Create a success response from text or a JSON-serializable object */
|
|
17
|
+
export declare function success(data: string | object): ToolResponse;
|
|
18
|
+
/** Create an error response */
|
|
19
|
+
export declare function error(message: string): ToolResponse;
|
|
20
|
+
/** Create a validation error for a missing required field */
|
|
21
|
+
export declare function required(field: string): ToolResponse;
|
|
22
|
+
/**
|
|
23
|
+
* Create a success response with TOON-encoded payload.
|
|
24
|
+
*
|
|
25
|
+
* Encodes structured data using TOON (Token-Oriented Object Notation)
|
|
26
|
+
* for ~40-50% token reduction compared to JSON.stringify().
|
|
27
|
+
* Ideal for list/tabular responses (arrays of uniform objects).
|
|
28
|
+
*
|
|
29
|
+
* @param data - Any JSON-serializable value (objects, arrays, primitives)
|
|
30
|
+
* @param options - Optional TOON encode options (defaults: pipe delimiter)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // In a handler:
|
|
35
|
+
* const users = await db.listUsers();
|
|
36
|
+
* return toonSuccess(users);
|
|
37
|
+
*
|
|
38
|
+
* // With custom options:
|
|
39
|
+
* return toonSuccess(data, { delimiter: ',' });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function toonSuccess(data: unknown, options?: EncodeOptions): ToolResponse;
|
|
43
|
+
//# sourceMappingURL=ResponseHelper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResponseHelper.d.ts","sourceRoot":"","sources":["../../../src/framework/ResponseHelper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAM/D,iCAAiC;AACjC,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC/C,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAMD,wEAAwE;AACxE,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,YAAY,CAG3D;AAED,+BAA+B;AAC/B,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAEnD;AAED,6DAA6D;AAC7D,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,CAEpD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,CAIhF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response Helpers
|
|
3
|
+
*
|
|
4
|
+
* Universal MCP response builders. No API coupling.
|
|
5
|
+
* These produce the standard MCP ToolResponse format.
|
|
6
|
+
*/
|
|
7
|
+
import { encode } from '@toon-format/toon';
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// Response Builders
|
|
10
|
+
// ============================================================================
|
|
11
|
+
/** Create a success response from text or a JSON-serializable object */
|
|
12
|
+
export function success(data) {
|
|
13
|
+
const text = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
|
|
14
|
+
return { content: [{ type: "text", text }] };
|
|
15
|
+
}
|
|
16
|
+
/** Create an error response */
|
|
17
|
+
export function error(message) {
|
|
18
|
+
return { content: [{ type: "text", text: message }], isError: true };
|
|
19
|
+
}
|
|
20
|
+
/** Create a validation error for a missing required field */
|
|
21
|
+
export function required(field) {
|
|
22
|
+
return { content: [{ type: "text", text: `Error: ${field} required` }], isError: true };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a success response with TOON-encoded payload.
|
|
26
|
+
*
|
|
27
|
+
* Encodes structured data using TOON (Token-Oriented Object Notation)
|
|
28
|
+
* for ~40-50% token reduction compared to JSON.stringify().
|
|
29
|
+
* Ideal for list/tabular responses (arrays of uniform objects).
|
|
30
|
+
*
|
|
31
|
+
* @param data - Any JSON-serializable value (objects, arrays, primitives)
|
|
32
|
+
* @param options - Optional TOON encode options (defaults: pipe delimiter)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // In a handler:
|
|
37
|
+
* const users = await db.listUsers();
|
|
38
|
+
* return toonSuccess(users);
|
|
39
|
+
*
|
|
40
|
+
* // With custom options:
|
|
41
|
+
* return toonSuccess(data, { delimiter: ',' });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function toonSuccess(data, options) {
|
|
45
|
+
const defaults = { delimiter: '|' };
|
|
46
|
+
const text = encode(data, { ...defaults, ...options });
|
|
47
|
+
return { content: [{ type: "text", text }] };
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=ResponseHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResponseHelper.js","sourceRoot":"","sources":["../../../src/framework/ResponseHelper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,MAAM,EAAsB,MAAM,mBAAmB,CAAC;AAY/D,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E,wEAAwE;AACxE,MAAM,UAAU,OAAO,CAAC,IAAqB;IACzC,MAAM,IAAI,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAC7E,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACjD,CAAC;AAED,+BAA+B;AAC/B,MAAM,UAAU,KAAK,CAAC,OAAe;IACjC,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AACzE,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,QAAQ,CAAC,KAAa;IAClC,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,WAAW,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAC5F,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,WAAW,CAAC,IAAa,EAAE,OAAuB;IAC9D,MAAM,QAAQ,GAAkB,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;IACnD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACvD,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolBuilder — Interface for MCP Tool Builders
|
|
3
|
+
*
|
|
4
|
+
* Abstraction that allows ToolRegistry to accept any builder implementation,
|
|
5
|
+
* not just GroupedToolBuilder. This follows the Dependency Inversion Principle.
|
|
6
|
+
*
|
|
7
|
+
* @template TContext - The context type passed to handlers on each call.
|
|
8
|
+
*/
|
|
9
|
+
import type { Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import type { ToolResponse } from './ResponseHelper.js';
|
|
11
|
+
/** Metadata for a single action (used for enterprise observability / introspection) */
|
|
12
|
+
export interface ActionMetadata {
|
|
13
|
+
/** Full action key (e.g. "admin.create" for grouped, "list" for flat) */
|
|
14
|
+
key: string;
|
|
15
|
+
/** Action name within its group */
|
|
16
|
+
actionName: string;
|
|
17
|
+
/** Group name (undefined for flat actions) */
|
|
18
|
+
groupName?: string;
|
|
19
|
+
/** Human-readable description */
|
|
20
|
+
description?: string;
|
|
21
|
+
/** Whether this action is destructive */
|
|
22
|
+
destructive: boolean;
|
|
23
|
+
/** Whether this action is idempotent */
|
|
24
|
+
idempotent: boolean;
|
|
25
|
+
/** Whether this action is read-only */
|
|
26
|
+
readOnly: boolean;
|
|
27
|
+
/** Required field names from the Zod schema */
|
|
28
|
+
requiredFields: string[];
|
|
29
|
+
/** Whether this action has group/action-level middleware */
|
|
30
|
+
hasMiddleware: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface ToolBuilder<TContext = void> {
|
|
33
|
+
/** Get the tool name (used as the registration key) */
|
|
34
|
+
getName(): string;
|
|
35
|
+
/** Get the capability tags for selective exposure */
|
|
36
|
+
getTags(): string[];
|
|
37
|
+
/** Get all registered action keys */
|
|
38
|
+
getActionNames(): string[];
|
|
39
|
+
/** Get metadata for all registered actions (for enterprise observability) */
|
|
40
|
+
getActionMetadata(): ActionMetadata[];
|
|
41
|
+
/** Build and return the MCP Tool definition. May cache internally. */
|
|
42
|
+
buildToolDefinition(): McpTool;
|
|
43
|
+
/** Execute a tool call with the given context and arguments */
|
|
44
|
+
execute(ctx: TContext, args: Record<string, unknown>): Promise<ToolResponse>;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=ToolBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolBuilder.d.ts","sourceRoot":"","sources":["../../../src/framework/ToolBuilder.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAC1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,uFAAuF;AACvF,MAAM,WAAW,cAAc;IAC3B,yEAAyE;IACzE,GAAG,EAAE,MAAM,CAAC;IACZ,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,8CAA8C;IAC9C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iCAAiC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,yCAAyC;IACzC,WAAW,EAAE,OAAO,CAAC;IACrB,wCAAwC;IACxC,UAAU,EAAE,OAAO,CAAC;IACpB,uCAAuC;IACvC,QAAQ,EAAE,OAAO,CAAC;IAClB,+CAA+C;IAC/C,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,4DAA4D;IAC5D,aAAa,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW,CAAC,QAAQ,GAAG,IAAI;IACxC,uDAAuD;IACvD,OAAO,IAAI,MAAM,CAAC;IAElB,qDAAqD;IACrD,OAAO,IAAI,MAAM,EAAE,CAAC;IAEpB,qCAAqC;IACrC,cAAc,IAAI,MAAM,EAAE,CAAC;IAE3B,6EAA6E;IAC7E,iBAAiB,IAAI,cAAc,EAAE,CAAC;IAEtC,sEAAsE;IACtE,mBAAmB,IAAI,OAAO,CAAC;IAE/B,+DAA+D;IAC/D,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAChF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolBuilder.js","sourceRoot":"","sources":["../../../src/framework/ToolBuilder.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolRegistry — Centralized Tool Registration & Routing
|
|
3
|
+
*
|
|
4
|
+
* Supports selective exposure via tags for reducing LLM context.
|
|
5
|
+
* Provides `attachToServer()` for 1-line MCP SDK integration.
|
|
6
|
+
*/
|
|
7
|
+
import type { Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
+
import { type ToolResponse } from './ResponseHelper.js';
|
|
9
|
+
import type { ToolBuilder } from './ToolBuilder.js';
|
|
10
|
+
/** Filter options for getTools() */
|
|
11
|
+
export interface ToolFilter {
|
|
12
|
+
/** Only include tools that have ALL these tags */
|
|
13
|
+
tags?: string[];
|
|
14
|
+
/** Exclude tools that have ANY of these tags */
|
|
15
|
+
exclude?: string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Options for attaching a ToolRegistry to an MCP Server.
|
|
19
|
+
*
|
|
20
|
+
* @template TContext - The context type used by the registry.
|
|
21
|
+
*/
|
|
22
|
+
export interface AttachOptions<TContext> {
|
|
23
|
+
/** Only expose tools matching these tag filters */
|
|
24
|
+
filter?: ToolFilter;
|
|
25
|
+
/**
|
|
26
|
+
* Factory function to create a per-request context.
|
|
27
|
+
* Receives the MCP `extra` object (session info, meta, etc.).
|
|
28
|
+
* If omitted, `undefined` is used as context (suitable for `ToolRegistry<void>`).
|
|
29
|
+
*/
|
|
30
|
+
contextFactory?: (extra: unknown) => TContext;
|
|
31
|
+
}
|
|
32
|
+
/** Function to detach the registry from the server */
|
|
33
|
+
export type DetachFn = () => void;
|
|
34
|
+
export declare class ToolRegistry<TContext = void> {
|
|
35
|
+
private _builders;
|
|
36
|
+
/** Register a tool builder. Throws on duplicate name. */
|
|
37
|
+
register(builder: ToolBuilder<TContext>): void;
|
|
38
|
+
/** Register multiple builders at once */
|
|
39
|
+
registerAll(...builders: ToolBuilder<TContext>[]): void;
|
|
40
|
+
/** Get all tool definitions */
|
|
41
|
+
getAllTools(): McpTool[];
|
|
42
|
+
/** Get tool definitions filtered by tags */
|
|
43
|
+
getTools(filter: ToolFilter): McpTool[];
|
|
44
|
+
/** Route a call to the correct builder */
|
|
45
|
+
routeCall(ctx: TContext, name: string, args: Record<string, unknown>): Promise<ToolResponse>;
|
|
46
|
+
/**
|
|
47
|
+
* Attach this registry to an MCP Server — 1-line integration.
|
|
48
|
+
*
|
|
49
|
+
* Supports both:
|
|
50
|
+
* - `Server` (low-level): `import { Server } from '@modelcontextprotocol/sdk/server/index.js'`
|
|
51
|
+
* - `McpServer` (high-level): `import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'`
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* // Minimal — void context
|
|
56
|
+
* registry.attachToServer(server);
|
|
57
|
+
*
|
|
58
|
+
* // With tag filtering
|
|
59
|
+
* registry.attachToServer(server, { filter: { tags: ['public'] } });
|
|
60
|
+
*
|
|
61
|
+
* // With per-request context
|
|
62
|
+
* registry.attachToServer(server, {
|
|
63
|
+
* contextFactory: (extra) => ({ session: extra })
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @returns A detach function to remove the handlers.
|
|
68
|
+
*/
|
|
69
|
+
attachToServer(server: unknown, options?: AttachOptions<TContext>): DetachFn;
|
|
70
|
+
/** Check if a tool is registered */
|
|
71
|
+
has(name: string): boolean;
|
|
72
|
+
/** Remove all registered tools */
|
|
73
|
+
clear(): void;
|
|
74
|
+
/** Get count of registered tools */
|
|
75
|
+
get size(): number;
|
|
76
|
+
/**
|
|
77
|
+
* Resolve the low-level Server instance from either a `Server` or `McpServer`.
|
|
78
|
+
*
|
|
79
|
+
* Uses duck-typing to detect the server type:
|
|
80
|
+
* - `McpServer` has a `.server` property that exposes the low-level `Server`
|
|
81
|
+
* - `Server` has `setRequestHandler` directly
|
|
82
|
+
*/
|
|
83
|
+
private _resolveServer;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=ToolRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolRegistry.d.ts","sourceRoot":"","sources":["../../../src/framework/ToolRegistry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAK1E,OAAO,EAAE,KAAK,YAAY,EAAS,MAAM,qBAAqB,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAMpD,oCAAoC;AACpC,MAAM,WAAW,UAAU;IACvB,kDAAkD;IAClD,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,gDAAgD;IAChD,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAWD;;;;GAIG;AACH,MAAM,WAAW,aAAa,CAAC,QAAQ;IACnC,mDAAmD;IACnD,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;OAIG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,QAAQ,CAAC;CACjD;AAED,sDAAsD;AACtD,MAAM,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC;AAMlC,qBAAa,YAAY,C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