@ibm/ibmi-mcp-server 0.2.0 → 0.3.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/README.md +140 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +14 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/resolver.d.ts.map +1 -1
- package/dist/config/resolver.js.map +1 -1
- package/dist/ibmi-mcp-server/tools/executeSql.tool.d.ts +80 -0
- package/dist/ibmi-mcp-server/tools/executeSql.tool.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/tools/executeSql.tool.js +356 -0
- package/dist/ibmi-mcp-server/tools/executeSql.tool.js.map +1 -0
- package/dist/ibmi-mcp-server/tools/generateSql.tool.d.ts +36 -0
- package/dist/ibmi-mcp-server/tools/generateSql.tool.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/tools/generateSql.tool.js +281 -0
- package/dist/ibmi-mcp-server/tools/generateSql.tool.js.map +1 -0
- package/dist/ibmi-mcp-server/tools/index.d.ts +85 -0
- package/dist/ibmi-mcp-server/tools/index.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/tools/index.js +21 -0
- package/dist/ibmi-mcp-server/tools/index.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/config/configParser.d.ts +9 -0
- package/dist/ibmi-mcp-server/utils/config/configParser.d.ts.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/configParser.js +58 -0
- package/dist/ibmi-mcp-server/utils/config/configParser.js.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolDefinitions.d.ts +4 -4
- package/dist/ibmi-mcp-server/utils/config/toolDefinitions.d.ts.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolDefinitions.js +7 -8
- package/dist/ibmi-mcp-server/utils/config/toolDefinitions.js.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolFactory.d.ts +0 -8
- package/dist/ibmi-mcp-server/utils/config/toolFactory.d.ts.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolFactory.js +0 -31
- package/dist/ibmi-mcp-server/utils/config/toolFactory.js.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolsetManager.d.ts +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolsetManager.d.ts.map +1 -1
- package/dist/ibmi-mcp-server/utils/config/toolsetManager.js +2 -2
- package/dist/ibmi-mcp-server/utils/config/toolsetManager.js.map +1 -1
- package/dist/ibmi-mcp-server/utils/language/document.d.ts +26 -0
- package/dist/ibmi-mcp-server/utils/language/document.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/document.js +318 -0
- package/dist/ibmi-mcp-server/utils/language/document.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/statement.d.ts +43 -0
- package/dist/ibmi-mcp-server/utils/language/statement.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/statement.js +796 -0
- package/dist/ibmi-mcp-server/utils/language/statement.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/tokens.d.ts +32 -0
- package/dist/ibmi-mcp-server/utils/language/tokens.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/tokens.js +532 -0
- package/dist/ibmi-mcp-server/utils/language/tokens.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/types.d.ts +138 -0
- package/dist/ibmi-mcp-server/utils/language/types.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/language/types.js +93 -0
- package/dist/ibmi-mcp-server/utils/language/types.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/security/ibmiSqlParser.d.ts +48 -0
- package/dist/ibmi-mcp-server/utils/security/ibmiSqlParser.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/security/ibmiSqlParser.js +93 -0
- package/dist/ibmi-mcp-server/utils/security/ibmiSqlParser.js.map +1 -0
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidator.d.ts +35 -59
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidator.d.ts.map +1 -1
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidator.js +108 -288
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidator.js.map +1 -1
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidatorFallback.d.ts +54 -0
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidatorFallback.d.ts.map +1 -0
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidatorFallback.js +91 -0
- package/dist/ibmi-mcp-server/utils/security/sqlSecurityValidatorFallback.js.map +1 -0
- package/dist/mcp-server/tools/index.d.ts +13 -2
- package/dist/mcp-server/tools/index.d.ts.map +1 -1
- package/dist/mcp-server/tools/index.js +29 -9
- package/dist/mcp-server/tools/index.js.map +1 -1
- package/dist/mcp-server/tools/utils/index.d.ts +7 -0
- package/dist/mcp-server/tools/utils/index.d.ts.map +1 -0
- package/dist/mcp-server/tools/utils/index.js +7 -0
- package/dist/mcp-server/tools/utils/index.js.map +1 -0
- package/dist/mcp-server/tools/utils/tool-factory.d.ts +129 -0
- package/dist/mcp-server/tools/utils/tool-factory.d.ts.map +1 -0
- package/dist/mcp-server/tools/utils/tool-factory.js +179 -0
- package/dist/mcp-server/tools/utils/tool-factory.js.map +1 -0
- package/dist/mcp-server/tools/utils/types.d.ts +117 -0
- package/dist/mcp-server/tools/utils/types.d.ts.map +1 -0
- package/dist/mcp-server/tools/utils/types.js +11 -0
- package/dist/mcp-server/tools/utils/types.js.map +1 -0
- package/dist/mcp-server/transports/core/cleanupTransformStream.d.ts +26 -0
- package/dist/mcp-server/transports/core/cleanupTransformStream.d.ts.map +1 -0
- package/dist/mcp-server/transports/core/cleanupTransformStream.js +60 -0
- package/dist/mcp-server/transports/core/cleanupTransformStream.js.map +1 -0
- package/dist/mcp-server/transports/core/statefulTransportManager.d.ts +1 -1
- package/dist/mcp-server/transports/core/statefulTransportManager.d.ts.map +1 -1
- package/dist/mcp-server/transports/core/statefulTransportManager.js +50 -40
- package/dist/mcp-server/transports/core/statefulTransportManager.js.map +1 -1
- package/dist/mcp-server/transports/core/statelessTransportManager.d.ts +2 -13
- package/dist/mcp-server/transports/core/statelessTransportManager.d.ts.map +1 -1
- package/dist/mcp-server/transports/core/statelessTransportManager.js +35 -62
- package/dist/mcp-server/transports/core/statelessTransportManager.js.map +1 -1
- package/dist/mcp-server/transports/core/transportRequest.d.ts +1 -2
- package/dist/mcp-server/transports/core/transportRequest.d.ts.map +1 -1
- package/dist/mcp-server/transports/http/mcpTransportMiddleware.d.ts.map +1 -1
- package/dist/mcp-server/transports/http/mcpTransportMiddleware.js +1 -3
- package/dist/mcp-server/transports/http/mcpTransportMiddleware.js.map +1 -1
- package/package.json +21 -22
- package/dist/ibmi-mcp-server/tools/executeSql/index.d.ts +0 -9
- package/dist/ibmi-mcp-server/tools/executeSql/index.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/executeSql/index.js +0 -9
- package/dist/ibmi-mcp-server/tools/executeSql/index.js.map +0 -1
- package/dist/ibmi-mcp-server/tools/executeSql/logic.d.ts +0 -51
- package/dist/ibmi-mcp-server/tools/executeSql/logic.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/executeSql/logic.js +0 -179
- package/dist/ibmi-mcp-server/tools/executeSql/logic.js.map +0 -1
- package/dist/ibmi-mcp-server/tools/executeSql/registration.d.ts +0 -52
- package/dist/ibmi-mcp-server/tools/executeSql/registration.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/executeSql/registration.js +0 -161
- package/dist/ibmi-mcp-server/tools/executeSql/registration.js.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/index.d.ts +0 -13
- package/dist/ibmi-mcp-server/tools/generateSql/index.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/index.js +0 -13
- package/dist/ibmi-mcp-server/tools/generateSql/index.js.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/logic.d.ts +0 -64
- package/dist/ibmi-mcp-server/tools/generateSql/logic.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/logic.js +0 -190
- package/dist/ibmi-mcp-server/tools/generateSql/logic.js.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/registration.d.ts +0 -18
- package/dist/ibmi-mcp-server/tools/generateSql/registration.d.ts.map +0 -1
- package/dist/ibmi-mcp-server/tools/generateSql/registration.js +0 -64
- package/dist/ibmi-mcp-server/tools/generateSql/registration.js.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/index.d.ts +0 -8
- package/dist/mcp-server/tools/catFactFetcher/index.d.ts.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/index.js +0 -8
- package/dist/mcp-server/tools/catFactFetcher/index.js.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/logic.d.ts +0 -52
- package/dist/mcp-server/tools/catFactFetcher/logic.d.ts.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/logic.js +0 -95
- package/dist/mcp-server/tools/catFactFetcher/logic.js.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/registration.d.ts +0 -9
- package/dist/mcp-server/tools/catFactFetcher/registration.d.ts.map +0 -1
- package/dist/mcp-server/tools/catFactFetcher/registration.js +0 -43
- package/dist/mcp-server/tools/catFactFetcher/registration.js.map +0 -1
- package/dist/mcp-server/tools/echoTool/index.d.ts +0 -13
- package/dist/mcp-server/tools/echoTool/index.d.ts.map +0 -1
- package/dist/mcp-server/tools/echoTool/index.js +0 -13
- package/dist/mcp-server/tools/echoTool/index.js.map +0 -1
- package/dist/mcp-server/tools/echoTool/logic.d.ts +0 -69
- package/dist/mcp-server/tools/echoTool/logic.d.ts.map +0 -1
- package/dist/mcp-server/tools/echoTool/logic.js +0 -119
- package/dist/mcp-server/tools/echoTool/logic.js.map +0 -1
- package/dist/mcp-server/tools/echoTool/registration.d.ts +0 -9
- package/dist/mcp-server/tools/echoTool/registration.d.ts.map +0 -1
- package/dist/mcp-server/tools/echoTool/registration.js +0 -45
- package/dist/mcp-server/tools/echoTool/registration.js.map +0 -1
- package/dist/mcp-server/tools/imageTest/index.d.ts +0 -7
- package/dist/mcp-server/tools/imageTest/index.d.ts.map +0 -1
- package/dist/mcp-server/tools/imageTest/index.js +0 -7
- package/dist/mcp-server/tools/imageTest/index.js.map +0 -1
- package/dist/mcp-server/tools/imageTest/logic.d.ts +0 -27
- package/dist/mcp-server/tools/imageTest/logic.d.ts.map +0 -1
- package/dist/mcp-server/tools/imageTest/logic.js +0 -44
- package/dist/mcp-server/tools/imageTest/logic.js.map +0 -1
- package/dist/mcp-server/tools/imageTest/registration.d.ts +0 -9
- package/dist/mcp-server/tools/imageTest/registration.d.ts.map +0 -1
- package/dist/mcp-server/tools/imageTest/registration.js +0 -49
- package/dist/mcp-server/tools/imageTest/registration.js.map +0 -1
- package/dist/mcp-server/tools/utils/tool-utils.d.ts +0 -43
- package/dist/mcp-server/tools/utils/tool-utils.d.ts.map +0 -1
- package/dist/mcp-server/tools/utils/tool-utils.js +0 -44
- package/dist/mcp-server/tools/utils/tool-utils.js.map +0 -1
- package/dist/mcp-server/transports/core/headerUtils.d.ts +0 -27
- package/dist/mcp-server/transports/core/headerUtils.d.ts.map +0 -1
- package/dist/mcp-server/transports/core/headerUtils.js +0 -53
- package/dist/mcp-server/transports/core/headerUtils.js.map +0 -1
- package/dist/mcp-server/transports/core/honoNodeBridge.d.ts +0 -77
- package/dist/mcp-server/transports/core/honoNodeBridge.d.ts.map +0 -1
- package/dist/mcp-server/transports/core/honoNodeBridge.js +0 -150
- package/dist/mcp-server/transports/core/honoNodeBridge.js.map +0 -1
- package/dist/services/duck-db/duckDBConnectionManager.d.ts +0 -55
- package/dist/services/duck-db/duckDBConnectionManager.d.ts.map +0 -1
- package/dist/services/duck-db/duckDBConnectionManager.js +0 -184
- package/dist/services/duck-db/duckDBConnectionManager.js.map +0 -1
- package/dist/services/duck-db/duckDBQueryExecutor.d.ts +0 -18
- package/dist/services/duck-db/duckDBQueryExecutor.d.ts.map +0 -1
- package/dist/services/duck-db/duckDBQueryExecutor.js +0 -115
- package/dist/services/duck-db/duckDBQueryExecutor.js.map +0 -1
- package/dist/services/duck-db/duckDBService.d.ts +0 -27
- package/dist/services/duck-db/duckDBService.d.ts.map +0 -1
- package/dist/services/duck-db/duckDBService.js +0 -151
- package/dist/services/duck-db/duckDBService.js.map +0 -1
- package/dist/services/duck-db/types.d.ts +0 -135
- package/dist/services/duck-db/types.d.ts.map +0 -1
- package/dist/services/duck-db/types.js +0 -6
- package/dist/services/duck-db/types.js.map +0 -1
- package/dist/services/llm-providers/openRouterProvider.d.ts +0 -36
- package/dist/services/llm-providers/openRouterProvider.d.ts.map +0 -1
- package/dist/services/llm-providers/openRouterProvider.js +0 -235
- package/dist/services/llm-providers/openRouterProvider.js.map +0 -1
- package/dist/services/supabase/supabaseClient.d.ts +0 -25
- package/dist/services/supabase/supabaseClient.d.ts.map +0 -1
- package/dist/services/supabase/supabaseClient.js +0 -68
- package/dist/services/supabase/supabaseClient.js.map +0 -1
- package/dist/storage/duckdbExample.d.ts +0 -8
- package/dist/storage/duckdbExample.d.ts.map +0 -1
- package/dist/storage/duckdbExample.js +0 -197
- package/dist/storage/duckdbExample.js.map +0 -1
|
@@ -1,4 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Registration Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Registers all factory pattern tools with the MCP server.
|
|
5
|
+
* Each tool is registered directly using registerToolFromDefinition.
|
|
6
|
+
*
|
|
7
|
+
* @module tools
|
|
8
|
+
*/
|
|
1
9
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
/**
|
|
3
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Registers all factory pattern tools with the MCP server.
|
|
12
|
+
* Tools are defined in the allToolDefinitions array.
|
|
13
|
+
*/
|
|
14
|
+
export declare function registerAllTools(server: McpServer): Promise<void>;
|
|
4
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mcp-server/tools/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mcp-server/tools/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAUzE;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CA0BvE"}
|
|
@@ -1,13 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tool Registration Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Registers all factory pattern tools with the MCP server.
|
|
5
|
+
* Each tool is registered directly using registerToolFromDefinition.
|
|
6
|
+
*
|
|
7
|
+
* @module tools
|
|
8
|
+
*/
|
|
9
|
+
import { allToolDefinitions } from "../../ibmi-mcp-server/tools/index.js";
|
|
10
|
+
import { registerToolFromDefinition } from "./utils/tool-factory.js";
|
|
11
|
+
import { logger, requestContextService } from "../../utils/index.js";
|
|
2
12
|
import { logOperationStart, logOperationSuccess, } from "../../utils/internal/logging-helpers.js";
|
|
3
|
-
import {
|
|
4
|
-
/**
|
|
5
|
-
|
|
13
|
+
import { config } from "../../config/index.js";
|
|
14
|
+
/**
|
|
15
|
+
* Registers all factory pattern tools with the MCP server.
|
|
16
|
+
* Tools are defined in the allToolDefinitions array.
|
|
17
|
+
*/
|
|
18
|
+
export async function registerAllTools(server) {
|
|
6
19
|
const context = requestContextService.createRequestContext({
|
|
7
|
-
operation: "
|
|
20
|
+
operation: "RegisterAllTools",
|
|
8
21
|
});
|
|
9
|
-
logOperationStart(context,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
logOperationStart(context, `Registering ${allToolDefinitions.length} factory pattern tools`);
|
|
23
|
+
for (const toolDef of allToolDefinitions) {
|
|
24
|
+
if (toolDef.name === "execute_sql" && !config.ibmi_enableExecuteSql) {
|
|
25
|
+
// Skip execute_sql tool if disabled via IBMI_ENABLE_EXECUTE_SQL
|
|
26
|
+
logger.debug(context, "Skipping registration of execute_sql tool (IBMI_ENABLE_EXECUTE_SQL=false)");
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
await registerToolFromDefinition(server, toolDef);
|
|
30
|
+
}
|
|
31
|
+
logOperationSuccess(context, `Successfully registered ${allToolDefinitions.length} factory pattern tools`);
|
|
32
|
+
}
|
|
13
33
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/mcp-server/tools/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/mcp-server/tools/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EACL,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,MAAiB;IACtD,MAAM,OAAO,GAAG,qBAAqB,CAAC,oBAAoB,CAAC;QACzD,SAAS,EAAE,kBAAkB;KAC9B,CAAC,CAAC;IAEH,iBAAiB,CACf,OAAO,EACP,eAAe,kBAAkB,CAAC,MAAM,wBAAwB,CACjE,CAAC;IAEF,KAAK,MAAM,OAAO,IAAI,kBAAkB,EAAE,CAAC;QACzC,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,IAAI,CAAC,MAAM,CAAC,qBAAqB,EAAE,CAAC;YACpE,gEAAgE;YAChE,MAAM,CAAC,KAAK,CACV,OAAO,EACP,2EAA2E,CAC5E,CAAC;YACF,SAAS;QACX,CAAC;QACD,MAAM,0BAA0B,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpD,CAAC;IAED,mBAAmB,CACjB,OAAO,EACP,2BAA2B,kBAAkB,CAAC,MAAM,wBAAwB,CAC7E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/mcp-server/tools/utils/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,YAAY,EACV,cAAc,EACd,eAAe,EACf,WAAW,EACX,UAAU,GACX,MAAM,YAAY,CAAC;AAGpB,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/mcp-server/tools/utils/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,2BAA2B;AAC3B,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Registration Factory
|
|
3
|
+
*
|
|
4
|
+
* Provides a factory pattern for creating and registering MCP tools with minimal boilerplate.
|
|
5
|
+
* Handles validation, registration, error handling, and response formatting automatically.
|
|
6
|
+
*
|
|
7
|
+
* @module tool-factory
|
|
8
|
+
* @feature 001-tool-factory
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
12
|
+
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
import { type RequestContext } from "../../../utils/index.js";
|
|
14
|
+
import type { ToolDefinition, ResponseFormatter, SdkContext } from "./types.js";
|
|
15
|
+
/**
|
|
16
|
+
* Zod schema for validating tool definitions at registration time.
|
|
17
|
+
*/
|
|
18
|
+
export declare const ToolDefinitionSchema: z.ZodObject<{
|
|
19
|
+
name: z.ZodString;
|
|
20
|
+
description: z.ZodString;
|
|
21
|
+
inputSchema: z.ZodType<z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
22
|
+
[x: string]: any;
|
|
23
|
+
}, {
|
|
24
|
+
[x: string]: any;
|
|
25
|
+
}>, z.ZodTypeDef, z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
26
|
+
[x: string]: any;
|
|
27
|
+
}, {
|
|
28
|
+
[x: string]: any;
|
|
29
|
+
}>>;
|
|
30
|
+
outputSchema: z.ZodType<z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
31
|
+
[x: string]: any;
|
|
32
|
+
}, {
|
|
33
|
+
[x: string]: any;
|
|
34
|
+
}>, z.ZodTypeDef, z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
35
|
+
[x: string]: any;
|
|
36
|
+
}, {
|
|
37
|
+
[x: string]: any;
|
|
38
|
+
}>>;
|
|
39
|
+
logic: z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>;
|
|
40
|
+
title: z.ZodOptional<z.ZodString>;
|
|
41
|
+
responseFormatter: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>;
|
|
42
|
+
annotations: z.ZodOptional<z.ZodObject<{
|
|
43
|
+
readOnlyHint: z.ZodOptional<z.ZodBoolean>;
|
|
44
|
+
destructiveHint: z.ZodOptional<z.ZodBoolean>;
|
|
45
|
+
openWorldHint: z.ZodOptional<z.ZodBoolean>;
|
|
46
|
+
}, "strip", z.ZodTypeAny, {
|
|
47
|
+
readOnlyHint?: boolean | undefined;
|
|
48
|
+
openWorldHint?: boolean | undefined;
|
|
49
|
+
destructiveHint?: boolean | undefined;
|
|
50
|
+
}, {
|
|
51
|
+
readOnlyHint?: boolean | undefined;
|
|
52
|
+
openWorldHint?: boolean | undefined;
|
|
53
|
+
destructiveHint?: boolean | undefined;
|
|
54
|
+
}>>;
|
|
55
|
+
enabled: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>]>>;
|
|
56
|
+
}, "strip", z.ZodTypeAny, {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
inputSchema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
60
|
+
[x: string]: any;
|
|
61
|
+
}, {
|
|
62
|
+
[x: string]: any;
|
|
63
|
+
}>;
|
|
64
|
+
outputSchema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
65
|
+
[x: string]: any;
|
|
66
|
+
}, {
|
|
67
|
+
[x: string]: any;
|
|
68
|
+
}>;
|
|
69
|
+
logic: (...args: unknown[]) => unknown;
|
|
70
|
+
enabled?: boolean | ((...args: unknown[]) => unknown) | undefined;
|
|
71
|
+
title?: string | undefined;
|
|
72
|
+
annotations?: {
|
|
73
|
+
readOnlyHint?: boolean | undefined;
|
|
74
|
+
openWorldHint?: boolean | undefined;
|
|
75
|
+
destructiveHint?: boolean | undefined;
|
|
76
|
+
} | undefined;
|
|
77
|
+
responseFormatter?: ((...args: unknown[]) => unknown) | undefined;
|
|
78
|
+
}, {
|
|
79
|
+
name: string;
|
|
80
|
+
description: string;
|
|
81
|
+
inputSchema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
82
|
+
[x: string]: any;
|
|
83
|
+
}, {
|
|
84
|
+
[x: string]: any;
|
|
85
|
+
}>;
|
|
86
|
+
outputSchema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
87
|
+
[x: string]: any;
|
|
88
|
+
}, {
|
|
89
|
+
[x: string]: any;
|
|
90
|
+
}>;
|
|
91
|
+
logic: (...args: unknown[]) => unknown;
|
|
92
|
+
enabled?: boolean | ((...args: unknown[]) => unknown) | undefined;
|
|
93
|
+
title?: string | undefined;
|
|
94
|
+
annotations?: {
|
|
95
|
+
readOnlyHint?: boolean | undefined;
|
|
96
|
+
openWorldHint?: boolean | undefined;
|
|
97
|
+
destructiveHint?: boolean | undefined;
|
|
98
|
+
} | undefined;
|
|
99
|
+
responseFormatter?: ((...args: unknown[]) => unknown) | undefined;
|
|
100
|
+
}>;
|
|
101
|
+
/**
|
|
102
|
+
* Creates a standardized MCP tool handler.
|
|
103
|
+
* This factory encapsulates context creation, performance measurement,
|
|
104
|
+
* error handling, and response formatting. It separates the app's internal
|
|
105
|
+
* RequestContext from the SDK's context (SdkContext).
|
|
106
|
+
*
|
|
107
|
+
* @param toolName - The name of the tool for logging and metrics
|
|
108
|
+
* @param logic - The core business logic function
|
|
109
|
+
* @param responseFormatter - Optional custom formatter (defaults to JSON)
|
|
110
|
+
* @returns A handler function compatible with the MCP SDK's tool callback
|
|
111
|
+
*/
|
|
112
|
+
export declare function createHandler<TInput, TOutput>(toolName: string, logic: (input: TInput, appContext: RequestContext, sdkContext: SdkContext) => Promise<TOutput>, responseFormatter?: ResponseFormatter<TOutput>): (input: TInput, extra: unknown) => Promise<CallToolResult>;
|
|
113
|
+
/**
|
|
114
|
+
* Factory function for creating tool definitions.
|
|
115
|
+
* Provides type inference and acts as the main entry point for tool authors.
|
|
116
|
+
*
|
|
117
|
+
* @param definition - The tool configuration
|
|
118
|
+
* @returns The same tool definition (for type inference)
|
|
119
|
+
*/
|
|
120
|
+
export declare function defineTool<TInputShape extends z.ZodRawShape, TOutputShape extends z.ZodRawShape>(definition: ToolDefinition<TInputShape, TOutputShape>): ToolDefinition<TInputShape, TOutputShape>;
|
|
121
|
+
/**
|
|
122
|
+
* Registers a single tool from its definition.
|
|
123
|
+
* Accepts any tool definition shape to support arrays of mixed tool types.
|
|
124
|
+
*
|
|
125
|
+
* @param server - The MCP server instance
|
|
126
|
+
* @param toolDef - The tool definition to register
|
|
127
|
+
*/
|
|
128
|
+
export declare function registerToolFromDefinition(server: McpServer, toolDef: ToolDefinition<any, any>): Promise<void>;
|
|
129
|
+
//# sourceMappingURL=tool-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-factory.d.ts","sourceRoot":"","sources":["../../../../src/mcp-server/tools/utils/tool-factory.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAa,MAAM,KAAK,CAAC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACzE,OAAO,KAAK,EACV,cAAc,EAEf,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAIL,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAC;AAKjC,OAAO,KAAK,EACV,cAAc,EAEd,iBAAiB,EACjB,UAAU,EACX,MAAM,YAAY,CAAC;AAMpB;;GAEG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgC/B,CAAC;AAkBH;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,EAC3C,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,CACL,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,cAAc,EAC1B,UAAU,EAAE,UAAU,KACnB,OAAO,CAAC,OAAO,CAAC,EACrB,iBAAiB,GAAE,iBAAiB,CAAC,OAAO,CAA0D,GACrG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,cAAc,CAAC,CAiD5D;AAMD;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,WAAW,SAAS,CAAC,CAAC,WAAW,EACjC,YAAY,SAAS,CAAC,CAAC,WAAW,EAElC,UAAU,EAAE,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,GACpD,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,CAI3C;AAED;;;;;;GAMG;AACH,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,SAAS,EAEjB,OAAO,EAAE,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,GAChC,OAAO,CAAC,IAAI,CAAC,CA6Ef"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Registration Factory
|
|
3
|
+
*
|
|
4
|
+
* Provides a factory pattern for creating and registering MCP tools with minimal boilerplate.
|
|
5
|
+
* Handles validation, registration, error handling, and response formatting automatically.
|
|
6
|
+
*
|
|
7
|
+
* @module tool-factory
|
|
8
|
+
* @feature 001-tool-factory
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { JsonRpcErrorCode } from "../../../types-global/errors.js";
|
|
12
|
+
import { ErrorHandler, requestContextService, measureToolExecution, } from "../../../utils/index.js";
|
|
13
|
+
import { logOperationStart, logOperationSuccess, } from "../../../utils/internal/logging-helpers.js";
|
|
14
|
+
// =============================================================================
|
|
15
|
+
// Validation Schema
|
|
16
|
+
// =============================================================================
|
|
17
|
+
/**
|
|
18
|
+
* Zod schema for validating tool definitions at registration time.
|
|
19
|
+
*/
|
|
20
|
+
export const ToolDefinitionSchema = z.object({
|
|
21
|
+
name: z
|
|
22
|
+
.string()
|
|
23
|
+
.min(1, "Tool name is required")
|
|
24
|
+
.regex(/^[a-z][a-z0-9_]*$/, "Tool name must be snake_case (lowercase letters, numbers, underscores)"),
|
|
25
|
+
description: z.string().min(1, "Tool description is required"),
|
|
26
|
+
inputSchema: z.custom((val) => val instanceof z.ZodObject, {
|
|
27
|
+
message: "inputSchema must be a Zod object schema",
|
|
28
|
+
}),
|
|
29
|
+
outputSchema: z.custom((val) => val instanceof z.ZodObject, {
|
|
30
|
+
message: "outputSchema must be a Zod object schema",
|
|
31
|
+
}),
|
|
32
|
+
logic: z.function(),
|
|
33
|
+
title: z.string().optional(),
|
|
34
|
+
responseFormatter: z.function().optional(),
|
|
35
|
+
annotations: z
|
|
36
|
+
.object({
|
|
37
|
+
readOnlyHint: z.boolean().optional(),
|
|
38
|
+
destructiveHint: z.boolean().optional(),
|
|
39
|
+
openWorldHint: z.boolean().optional(),
|
|
40
|
+
})
|
|
41
|
+
.optional(),
|
|
42
|
+
enabled: z.union([z.boolean(), z.function()]).optional(),
|
|
43
|
+
});
|
|
44
|
+
// =============================================================================
|
|
45
|
+
// Default Response Formatter
|
|
46
|
+
// =============================================================================
|
|
47
|
+
/**
|
|
48
|
+
* Default formatter for successful responses.
|
|
49
|
+
* Returns a simple JSON representation of the result.
|
|
50
|
+
*/
|
|
51
|
+
const defaultResponseFormatter = (result) => [
|
|
52
|
+
{ type: "text", text: JSON.stringify(result, null, 2) },
|
|
53
|
+
];
|
|
54
|
+
// =============================================================================
|
|
55
|
+
// Handler Factory
|
|
56
|
+
// =============================================================================
|
|
57
|
+
/**
|
|
58
|
+
* Creates a standardized MCP tool handler.
|
|
59
|
+
* This factory encapsulates context creation, performance measurement,
|
|
60
|
+
* error handling, and response formatting. It separates the app's internal
|
|
61
|
+
* RequestContext from the SDK's context (SdkContext).
|
|
62
|
+
*
|
|
63
|
+
* @param toolName - The name of the tool for logging and metrics
|
|
64
|
+
* @param logic - The core business logic function
|
|
65
|
+
* @param responseFormatter - Optional custom formatter (defaults to JSON)
|
|
66
|
+
* @returns A handler function compatible with the MCP SDK's tool callback
|
|
67
|
+
*/
|
|
68
|
+
export function createHandler(toolName, logic, responseFormatter = defaultResponseFormatter) {
|
|
69
|
+
return async (input, callContext) => {
|
|
70
|
+
// Cast the SDK context to our specific SdkContext type
|
|
71
|
+
const sdkContext = callContext;
|
|
72
|
+
const sessionId = typeof sdkContext?.sessionId === "string"
|
|
73
|
+
? sdkContext.sessionId
|
|
74
|
+
: undefined;
|
|
75
|
+
// Create the application's internal logger/tracing context
|
|
76
|
+
const appContext = requestContextService.createRequestContext({
|
|
77
|
+
parentContext: sdkContext,
|
|
78
|
+
operation: "HandleToolRequest",
|
|
79
|
+
additionalContext: { toolName, sessionId, input },
|
|
80
|
+
});
|
|
81
|
+
try {
|
|
82
|
+
const result = await measureToolExecution(toolName, () => logic(input, appContext, sdkContext), input);
|
|
83
|
+
return {
|
|
84
|
+
structuredContent: result,
|
|
85
|
+
content: responseFormatter(result),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
const mcpError = ErrorHandler.handleError(error, {
|
|
90
|
+
operation: `tool:${toolName}`,
|
|
91
|
+
context: appContext,
|
|
92
|
+
input,
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
isError: true,
|
|
96
|
+
content: [{ type: "text", text: `Error: ${mcpError.message}` }],
|
|
97
|
+
structuredContent: {
|
|
98
|
+
code: mcpError.code,
|
|
99
|
+
message: mcpError.message,
|
|
100
|
+
details: mcpError.details,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
// =============================================================================
|
|
107
|
+
// Factory Functions
|
|
108
|
+
// =============================================================================
|
|
109
|
+
/**
|
|
110
|
+
* Factory function for creating tool definitions.
|
|
111
|
+
* Provides type inference and acts as the main entry point for tool authors.
|
|
112
|
+
*
|
|
113
|
+
* @param definition - The tool configuration
|
|
114
|
+
* @returns The same tool definition (for type inference)
|
|
115
|
+
*/
|
|
116
|
+
export function defineTool(definition) {
|
|
117
|
+
// Validate the definition
|
|
118
|
+
ToolDefinitionSchema.parse(definition);
|
|
119
|
+
return definition;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Registers a single tool from its definition.
|
|
123
|
+
* Accepts any tool definition shape to support arrays of mixed tool types.
|
|
124
|
+
*
|
|
125
|
+
* @param server - The MCP server instance
|
|
126
|
+
* @param toolDef - The tool definition to register
|
|
127
|
+
*/
|
|
128
|
+
export async function registerToolFromDefinition(server,
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
|
+
toolDef) {
|
|
131
|
+
const registrationContext = requestContextService.createRequestContext({
|
|
132
|
+
operation: "RegisterTool",
|
|
133
|
+
toolName: toolDef.name,
|
|
134
|
+
});
|
|
135
|
+
logOperationStart(registrationContext, `Registering tool: '${toolDef.name}'`);
|
|
136
|
+
await ErrorHandler.tryCatch(async () => {
|
|
137
|
+
// Check if tool is enabled
|
|
138
|
+
const enabled = toolDef.enabled === undefined
|
|
139
|
+
? true
|
|
140
|
+
: typeof toolDef.enabled === "function"
|
|
141
|
+
? toolDef.enabled()
|
|
142
|
+
: toolDef.enabled;
|
|
143
|
+
if (!enabled) {
|
|
144
|
+
logOperationSuccess(registrationContext, `Tool '${toolDef.name}' is disabled, skipping registration`);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// Prepare metadata
|
|
148
|
+
const title = toolDef.title ||
|
|
149
|
+
toolDef.name
|
|
150
|
+
.split("_")
|
|
151
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
152
|
+
.join(" ");
|
|
153
|
+
const annotations = {
|
|
154
|
+
readOnlyHint: toolDef.annotations?.readOnlyHint ?? false,
|
|
155
|
+
destructiveHint: toolDef.annotations?.destructiveHint ?? false,
|
|
156
|
+
openWorldHint: toolDef.annotations?.openWorldHint ?? false,
|
|
157
|
+
};
|
|
158
|
+
// Use custom formatter or default
|
|
159
|
+
const responseFormatter = toolDef.responseFormatter || defaultResponseFormatter;
|
|
160
|
+
// Create handler using the factory
|
|
161
|
+
const handler = createHandler(toolDef.name, toolDef.logic, responseFormatter);
|
|
162
|
+
// Register with MCP server
|
|
163
|
+
// Use .shape to extract raw schema like YAML tools do
|
|
164
|
+
server.registerTool(toolDef.name, {
|
|
165
|
+
title,
|
|
166
|
+
description: toolDef.description,
|
|
167
|
+
inputSchema: toolDef.inputSchema.shape,
|
|
168
|
+
outputSchema: toolDef.outputSchema.shape,
|
|
169
|
+
annotations,
|
|
170
|
+
}, handler);
|
|
171
|
+
logOperationSuccess(registrationContext, `Tool '${toolDef.name}' registered successfully`);
|
|
172
|
+
}, {
|
|
173
|
+
operation: `RegisteringTool_${toolDef.name}`,
|
|
174
|
+
context: registrationContext,
|
|
175
|
+
errorCode: JsonRpcErrorCode.InitializationFailed,
|
|
176
|
+
critical: true,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=tool-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-factory.js","sourceRoot":"","sources":["../../../../src/mcp-server/tools/utils/tool-factory.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAa,MAAM,KAAK,CAAC;AAMnC,OAAO,EAAE,gBAAgB,EAAY,MAAM,iCAAiC,CAAC;AAC7E,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,oBAAoB,GAErB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,4CAA4C,CAAC;AAQpD,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3C,IAAI,EAAE,CAAC;SACJ,MAAM,EAAE;SACR,GAAG,CAAC,CAAC,EAAE,uBAAuB,CAAC;SAC/B,KAAK,CACJ,mBAAmB,EACnB,wEAAwE,CACzE;IACH,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,8BAA8B,CAAC;IAC9D,WAAW,EAAE,CAAC,CAAC,MAAM,CACnB,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,SAAS,EACnC;QACE,OAAO,EAAE,yCAAyC;KACnD,CACF;IACD,YAAY,EAAE,CAAC,CAAC,MAAM,CACpB,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,YAA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a,IAAI,KAAK;SAC3D,CAAC;QAEF,kCAAkC;QAClC,MAAM,iBAAiB,GACrB,OAAO,CAAC,iBAAiB,IAAI,wBAAwB,CAAC;QAExD,mCAAmC;QACnC,MAAM,OAAO,GAAG,aAAa,CAC3B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,KAAK,EACb,iBAAiB,CAClB,CAAC;QAEF,2BAA2B;QAC3B,sDAAsD;QACtD,MAAM,CAAC,YAAY,CACjB,OAAO,CAAC,IAAI,EACZ;YACE,KAAK;YACL,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,WAAW,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK;YACtC,YAAY,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK;YACxC,WAAW;SACZ,EACD,OAAO,CACR,CAAC;QAEF,mBAAmB,CACjB,mBAAmB,EACnB,SAAS,OAAO,CAAC,IAAI,2BAA2B,CACjD,CAAC;IACJ,CAAC,EACD;QACE,SAAS,EAAE,mBAAmB,OAAO,CAAC,IAAI,EAAE;QAC5C,OAAO,EAAE,mBAAmB;QAC5B,SAAS,EAAE,gBAAgB,CAAC,oBAAoB;QAChD,QAAQ,EAAE,IAAI;KACf,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Factory Contracts
|
|
3
|
+
*
|
|
4
|
+
* TypeScript interfaces defining the contract between tool authors
|
|
5
|
+
* and the registration system.
|
|
6
|
+
*
|
|
7
|
+
* @module tool-factory
|
|
8
|
+
* @feature 001-tool-factory
|
|
9
|
+
*/
|
|
10
|
+
import type { z, ZodObject, ZodRawShape } from "zod";
|
|
11
|
+
import type { ContentBlock } from "@modelcontextprotocol/sdk/types.js";
|
|
12
|
+
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
13
|
+
import type { ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
14
|
+
import type { RequestContext } from "../../../utils/index.js";
|
|
15
|
+
/**
|
|
16
|
+
* Defines the function signature for formatting a successful tool logic result
|
|
17
|
+
* into content blocks for display.
|
|
18
|
+
* @template TOutput The type of the successful output from the logic.
|
|
19
|
+
* @param result The successful output from the tool's logic function.
|
|
20
|
+
* @returns An array of ContentBlocks for the MCP client to display.
|
|
21
|
+
*/
|
|
22
|
+
export type ResponseFormatter<TOutput> = (result: TOutput) => ContentBlock[];
|
|
23
|
+
/**
|
|
24
|
+
* A type alias for the SDK's `RequestHandlerExtra` context, making it more
|
|
25
|
+
* specific and easier to reference in our tool logic signatures.
|
|
26
|
+
* Provides access to protocol-level capabilities like cancellation, notifications, and auth.
|
|
27
|
+
*/
|
|
28
|
+
export type SdkContext = RequestHandlerExtra<ServerRequest, ServerNotification>;
|
|
29
|
+
/**
|
|
30
|
+
* Function signature for tool business logic.
|
|
31
|
+
* Receives validated input, application context, and SDK context, returns structured output.
|
|
32
|
+
*
|
|
33
|
+
* @template TInput - Input type inferred from inputSchema
|
|
34
|
+
* @template TOutput - Output type inferred from outputSchema
|
|
35
|
+
*/
|
|
36
|
+
export type ToolLogicFn<TInput, TOutput> = (params: TInput, appContext: RequestContext, sdkContext: SdkContext) => Promise<TOutput>;
|
|
37
|
+
/**
|
|
38
|
+
* MCP protocol hints for LLM decision-making.
|
|
39
|
+
* All fields are optional and default to false.
|
|
40
|
+
*/
|
|
41
|
+
export interface ToolAnnotations {
|
|
42
|
+
/** Tool only reads data, no side effects */
|
|
43
|
+
readOnlyHint?: boolean;
|
|
44
|
+
/** Tool may permanently delete or modify data */
|
|
45
|
+
destructiveHint?: boolean;
|
|
46
|
+
/** Tool makes external API or network calls */
|
|
47
|
+
openWorldHint?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Developer-facing interface for declaring an MCP tool.
|
|
51
|
+
*
|
|
52
|
+
* @template TInputShape - Zod shape for input schema
|
|
53
|
+
* @template TOutputShape - Zod shape for output schema
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const myTool = defineTool({
|
|
58
|
+
* name: "my_tool",
|
|
59
|
+
* description: "Does something useful",
|
|
60
|
+
* inputSchema: z.object({ query: z.string() }),
|
|
61
|
+
* outputSchema: z.object({ result: z.string() }),
|
|
62
|
+
* logic: async ({ query }, appContext) => {
|
|
63
|
+
* logger.debug({ ...appContext, query }, 'Processing query');
|
|
64
|
+
* return { result: `Processed: ${query}` };
|
|
65
|
+
* },
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export interface ToolDefinition<TInputShape extends ZodRawShape = ZodRawShape, TOutputShape extends ZodRawShape = ZodRawShape> {
|
|
70
|
+
/**
|
|
71
|
+
* Unique tool identifier.
|
|
72
|
+
* Convention: snake_case (e.g., "describe_sql_object")
|
|
73
|
+
*/
|
|
74
|
+
name: string;
|
|
75
|
+
/**
|
|
76
|
+
* LLM-facing description of tool capabilities.
|
|
77
|
+
* Should clearly explain what the tool does and when to use it.
|
|
78
|
+
*/
|
|
79
|
+
description: string;
|
|
80
|
+
/**
|
|
81
|
+
* Zod schema validating tool input.
|
|
82
|
+
* Input is validated before logic function is called.
|
|
83
|
+
*/
|
|
84
|
+
inputSchema: ZodObject<TInputShape>;
|
|
85
|
+
/**
|
|
86
|
+
* Zod schema defining expected output structure.
|
|
87
|
+
* Used for documentation and type inference.
|
|
88
|
+
*/
|
|
89
|
+
outputSchema: ZodObject<TOutputShape>;
|
|
90
|
+
/**
|
|
91
|
+
* Pure async function implementing tool behavior.
|
|
92
|
+
* Receives validated input and application context, returns structured output.
|
|
93
|
+
*/
|
|
94
|
+
logic: ToolLogicFn<z.infer<ZodObject<TInputShape>>, z.infer<ZodObject<TOutputShape>>>;
|
|
95
|
+
/**
|
|
96
|
+
* Human-readable title for UI display.
|
|
97
|
+
* Defaults to formatted tool name if not provided.
|
|
98
|
+
*/
|
|
99
|
+
title?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Custom output formatter.
|
|
102
|
+
* Defaults to JSON serialization with text summary.
|
|
103
|
+
*/
|
|
104
|
+
responseFormatter?: ResponseFormatter<z.infer<ZodObject<TOutputShape>>>;
|
|
105
|
+
/**
|
|
106
|
+
* MCP safety hints for LLM decision-making.
|
|
107
|
+
* All hints default to false if not specified.
|
|
108
|
+
*/
|
|
109
|
+
annotations?: ToolAnnotations;
|
|
110
|
+
/**
|
|
111
|
+
* Conditional registration control.
|
|
112
|
+
* Set to false or return false from function to skip registration.
|
|
113
|
+
* Defaults to true (tool is registered).
|
|
114
|
+
*/
|
|
115
|
+
enabled?: boolean | (() => boolean);
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/mcp-server/tools/utils/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,CAAC,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACxF,OAAO,KAAK,EACV,kBAAkB,EAClB,aAAa,EACd,MAAM,oCAAoC,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAM9D;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,IAAI,CAAC,MAAM,EAAE,OAAO,KAAK,YAAY,EAAE,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,mBAAmB,CAAC,aAAa,EAAE,kBAAkB,CAAC,CAAC;AAEhF;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CAAC,MAAM,EAAE,OAAO,IAAI,CACzC,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,cAAc,EAC1B,UAAU,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
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a TransformStream wrapper that triggers cleanup when the stream completes.
|
|
3
|
+
*
|
|
4
|
+
* This utility wraps a ReadableStream with a transparent TransformStream that:
|
|
5
|
+
* - Passes through all chunks unchanged (identity transform)
|
|
6
|
+
* - Hooks into stream lifecycle events (flush, cancel, error)
|
|
7
|
+
* - Triggers cleanup automatically when the stream completes
|
|
8
|
+
* - Ensures cleanup happens exactly once
|
|
9
|
+
*
|
|
10
|
+
* @param originalStream - The ReadableStream to wrap
|
|
11
|
+
* @param onCleanup - Async function to call when the stream completes
|
|
12
|
+
* @returns A new ReadableStream that triggers cleanup on completion
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const wrappedStream = createCleanupTransformStream(
|
|
17
|
+
* responseStream,
|
|
18
|
+
* async () => {
|
|
19
|
+
* await server.close();
|
|
20
|
+
* await transport.close();
|
|
21
|
+
* }
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function createCleanupTransformStream(originalStream: ReadableStream, onCleanup: () => Promise<void>): ReadableStream;
|
|
26
|
+
//# sourceMappingURL=cleanupTransformStream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanupTransformStream.d.ts","sourceRoot":"","sources":["../../../../src/mcp-server/transports/core/cleanupTransformStream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,4BAA4B,CAC1C,cAAc,EAAE,cAAc,EAC9B,SAAS,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAC7B,cAAc,CAuChB"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a TransformStream wrapper that triggers cleanup when the stream completes.
|
|
3
|
+
*
|
|
4
|
+
* This utility wraps a ReadableStream with a transparent TransformStream that:
|
|
5
|
+
* - Passes through all chunks unchanged (identity transform)
|
|
6
|
+
* - Hooks into stream lifecycle events (flush, cancel, error)
|
|
7
|
+
* - Triggers cleanup automatically when the stream completes
|
|
8
|
+
* - Ensures cleanup happens exactly once
|
|
9
|
+
*
|
|
10
|
+
* @param originalStream - The ReadableStream to wrap
|
|
11
|
+
* @param onCleanup - Async function to call when the stream completes
|
|
12
|
+
* @returns A new ReadableStream that triggers cleanup on completion
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const wrappedStream = createCleanupTransformStream(
|
|
17
|
+
* responseStream,
|
|
18
|
+
* async () => {
|
|
19
|
+
* await server.close();
|
|
20
|
+
* await transport.close();
|
|
21
|
+
* }
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function createCleanupTransformStream(originalStream, onCleanup) {
|
|
26
|
+
// Track cleanup state to ensure idempotency
|
|
27
|
+
let cleanedUp = false;
|
|
28
|
+
const cleanup = async () => {
|
|
29
|
+
if (cleanedUp)
|
|
30
|
+
return;
|
|
31
|
+
cleanedUp = true;
|
|
32
|
+
await onCleanup();
|
|
33
|
+
};
|
|
34
|
+
// Create transform stream with lifecycle hooks
|
|
35
|
+
const { readable, writable } = new TransformStream({
|
|
36
|
+
/**
|
|
37
|
+
* Identity transform - passes chunks through unchanged
|
|
38
|
+
*/
|
|
39
|
+
transform(chunk, controller) {
|
|
40
|
+
controller.enqueue(chunk);
|
|
41
|
+
},
|
|
42
|
+
/**
|
|
43
|
+
* Called when stream closes normally (all data consumed)
|
|
44
|
+
*/
|
|
45
|
+
async flush() {
|
|
46
|
+
await cleanup();
|
|
47
|
+
},
|
|
48
|
+
/**
|
|
49
|
+
* Called when stream is cancelled/aborted by the consumer
|
|
50
|
+
*/
|
|
51
|
+
async cancel() {
|
|
52
|
+
await cleanup();
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
// Pipe original stream through transform
|
|
56
|
+
// If piping fails (error in original stream), trigger cleanup
|
|
57
|
+
originalStream.pipeTo(writable).catch(() => cleanup());
|
|
58
|
+
return readable;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=cleanupTransformStream.js.map
|