scorecard-ai-mcp 2.0.0 → 2.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/README.md +39 -0
- package/code-tool-paths.cjs +6 -0
- package/code-tool-paths.cjs.map +1 -0
- package/code-tool-paths.d.cts +2 -0
- package/code-tool-paths.d.cts.map +1 -0
- package/code-tool-types.d.mts +14 -0
- package/code-tool-types.d.mts.map +1 -0
- package/code-tool-types.d.ts +14 -0
- package/code-tool-types.d.ts.map +1 -0
- package/code-tool-types.js +4 -0
- package/code-tool-types.js.map +1 -0
- package/code-tool-types.mjs +3 -0
- package/code-tool-types.mjs.map +1 -0
- package/code-tool-worker.d.mts +5 -0
- package/code-tool-worker.d.mts.map +1 -0
- package/code-tool-worker.d.ts +5 -0
- package/code-tool-worker.d.ts.map +1 -0
- package/code-tool-worker.js +45 -0
- package/code-tool-worker.js.map +1 -0
- package/code-tool-worker.mjs +40 -0
- package/code-tool-worker.mjs.map +1 -0
- package/code-tool.d.mts +12 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +12 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +157 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +121 -0
- package/code-tool.mjs.map +1 -0
- package/compat.d.mts +4 -2
- package/compat.d.mts.map +1 -1
- package/compat.d.ts +4 -2
- package/compat.d.ts.map +1 -1
- package/compat.js +8 -3
- package/compat.js.map +1 -1
- package/compat.mjs +7 -2
- package/compat.mjs.map +1 -1
- package/filtering.d.mts +2 -0
- package/filtering.d.mts.map +1 -0
- package/filtering.d.ts +2 -0
- package/filtering.d.ts.map +1 -0
- package/filtering.js +20 -0
- package/filtering.js.map +1 -0
- package/filtering.mjs +13 -0
- package/filtering.mjs.map +1 -0
- package/headers.d.mts +4 -0
- package/headers.d.mts.map +1 -0
- package/headers.d.ts +4 -0
- package/headers.d.ts.map +1 -0
- package/headers.js +22 -0
- package/headers.js.map +1 -0
- package/headers.mjs +18 -0
- package/headers.mjs.map +1 -0
- package/http.d.mts +9 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +9 -0
- package/http.d.ts.map +1 -0
- package/http.js +108 -0
- package/http.js.map +1 -0
- package/http.mjs +100 -0
- package/http.mjs.map +1 -0
- package/index.js +15 -10
- package/index.js.map +1 -1
- package/index.mjs +17 -12
- package/index.mjs.map +1 -1
- package/options.d.mts +11 -6
- package/options.d.mts.map +1 -1
- package/options.d.ts +11 -6
- package/options.d.ts.map +1 -1
- package/options.js +136 -15
- package/options.js.map +1 -1
- package/options.mjs +135 -15
- package/options.mjs.map +1 -1
- package/package.json +89 -4
- package/server.d.mts +4 -16
- package/server.d.mts.map +1 -1
- package/server.d.ts +4 -16
- package/server.d.ts.map +1 -1
- package/server.js +86 -33
- package/server.js.map +1 -1
- package/server.mjs +84 -32
- package/server.mjs.map +1 -1
- package/src/code-tool-paths.cts +3 -0
- package/src/code-tool-types.ts +14 -0
- package/src/code-tool-worker.ts +46 -0
- package/src/code-tool.ts +146 -0
- package/src/compat.ts +9 -4
- package/src/filtering.ts +14 -0
- package/src/headers.ts +23 -0
- package/src/http.ts +127 -0
- package/src/index.ts +18 -14
- package/src/options.ts +158 -21
- package/src/server.ts +103 -48
- package/src/stdio.ts +13 -0
- package/src/tools/metrics/create-metrics.ts +8 -2
- package/src/tools/metrics/update-metrics.ts +8 -2
- package/src/tools/projects/create-projects.ts +14 -5
- package/src/tools/projects/list-projects.ts +16 -5
- package/src/tools/records/create-records.ts +17 -5
- package/src/tools/runs/create-runs.ts +14 -5
- package/src/tools/scores/upsert-scores.ts +16 -5
- package/src/tools/systems/delete-systems.ts +16 -5
- package/src/tools/systems/get-systems.ts +16 -5
- package/src/tools/systems/list-systems.ts +17 -5
- package/src/tools/systems/update-systems.ts +13 -5
- package/src/tools/systems/upsert-systems.ts +15 -5
- package/src/tools/systems/versions/get-systems-versions.ts +18 -5
- package/src/tools/systems/versions/upsert-systems-versions.ts +16 -5
- package/src/tools/testcases/create-testcases.ts +15 -5
- package/src/tools/testcases/delete-testcases.ts +14 -5
- package/src/tools/testcases/get-testcases.ts +16 -5
- package/src/tools/testcases/list-testcases.ts +17 -5
- package/src/tools/testcases/update-testcases.ts +17 -5
- package/src/tools/testsets/create-testsets.ts +14 -5
- package/src/tools/testsets/delete-testsets.ts +16 -5
- package/src/tools/testsets/get-testsets.ts +16 -5
- package/src/tools/testsets/list-testsets.ts +17 -5
- package/src/tools/testsets/update-testsets.ts +14 -5
- package/src/tools/types.ts +1 -1
- package/stdio.d.mts +3 -0
- package/stdio.d.mts.map +1 -0
- package/stdio.d.ts +3 -0
- package/stdio.d.ts.map +1 -0
- package/stdio.js +14 -0
- package/stdio.js.map +1 -0
- package/stdio.mjs +10 -0
- package/stdio.mjs.map +1 -0
- package/tools/metrics/create-metrics.d.mts +1 -1
- package/tools/metrics/create-metrics.d.mts.map +1 -1
- package/tools/metrics/create-metrics.d.ts +1 -1
- package/tools/metrics/create-metrics.d.ts.map +1 -1
- package/tools/metrics/create-metrics.js +7 -0
- package/tools/metrics/create-metrics.js.map +1 -1
- package/tools/metrics/create-metrics.mjs +7 -0
- package/tools/metrics/create-metrics.mjs.map +1 -1
- package/tools/metrics/update-metrics.d.mts +1 -1
- package/tools/metrics/update-metrics.d.mts.map +1 -1
- package/tools/metrics/update-metrics.d.ts +1 -1
- package/tools/metrics/update-metrics.d.ts.map +1 -1
- package/tools/metrics/update-metrics.js +7 -0
- package/tools/metrics/update-metrics.js.map +1 -1
- package/tools/metrics/update-metrics.mjs +7 -0
- package/tools/metrics/update-metrics.mjs.map +1 -1
- package/tools/projects/create-projects.d.mts +1 -1
- package/tools/projects/create-projects.d.mts.map +1 -1
- package/tools/projects/create-projects.d.ts +1 -1
- package/tools/projects/create-projects.d.ts.map +1 -1
- package/tools/projects/create-projects.js +11 -3
- package/tools/projects/create-projects.js.map +1 -1
- package/tools/projects/create-projects.mjs +11 -3
- package/tools/projects/create-projects.mjs.map +1 -1
- package/tools/projects/list-projects.d.mts +1 -1
- package/tools/projects/list-projects.d.mts.map +1 -1
- package/tools/projects/list-projects.d.ts +1 -1
- package/tools/projects/list-projects.d.ts.map +1 -1
- package/tools/projects/list-projects.js +14 -3
- package/tools/projects/list-projects.js.map +1 -1
- package/tools/projects/list-projects.mjs +14 -3
- package/tools/projects/list-projects.mjs.map +1 -1
- package/tools/records/create-records.d.mts +1 -1
- package/tools/records/create-records.d.mts.map +1 -1
- package/tools/records/create-records.d.ts +1 -1
- package/tools/records/create-records.d.ts.map +1 -1
- package/tools/records/create-records.js +14 -3
- package/tools/records/create-records.js.map +1 -1
- package/tools/records/create-records.mjs +14 -3
- package/tools/records/create-records.mjs.map +1 -1
- package/tools/runs/create-runs.d.mts +1 -1
- package/tools/runs/create-runs.d.mts.map +1 -1
- package/tools/runs/create-runs.d.ts +1 -1
- package/tools/runs/create-runs.d.ts.map +1 -1
- package/tools/runs/create-runs.js +11 -3
- package/tools/runs/create-runs.js.map +1 -1
- package/tools/runs/create-runs.mjs +11 -3
- package/tools/runs/create-runs.mjs.map +1 -1
- package/tools/scores/upsert-scores.d.mts +1 -1
- package/tools/scores/upsert-scores.d.mts.map +1 -1
- package/tools/scores/upsert-scores.d.ts +1 -1
- package/tools/scores/upsert-scores.d.ts.map +1 -1
- package/tools/scores/upsert-scores.js +14 -3
- package/tools/scores/upsert-scores.js.map +1 -1
- package/tools/scores/upsert-scores.mjs +14 -3
- package/tools/scores/upsert-scores.mjs.map +1 -1
- package/tools/systems/delete-systems.d.mts +1 -1
- package/tools/systems/delete-systems.d.mts.map +1 -1
- package/tools/systems/delete-systems.d.ts +1 -1
- package/tools/systems/delete-systems.d.ts.map +1 -1
- package/tools/systems/delete-systems.js +13 -3
- package/tools/systems/delete-systems.js.map +1 -1
- package/tools/systems/delete-systems.mjs +13 -3
- package/tools/systems/delete-systems.mjs.map +1 -1
- package/tools/systems/get-systems.d.mts +1 -1
- package/tools/systems/get-systems.d.mts.map +1 -1
- package/tools/systems/get-systems.d.ts +1 -1
- package/tools/systems/get-systems.d.ts.map +1 -1
- package/tools/systems/get-systems.js +13 -3
- package/tools/systems/get-systems.js.map +1 -1
- package/tools/systems/get-systems.mjs +13 -3
- package/tools/systems/get-systems.mjs.map +1 -1
- package/tools/systems/list-systems.d.mts +1 -1
- package/tools/systems/list-systems.d.mts.map +1 -1
- package/tools/systems/list-systems.d.ts +1 -1
- package/tools/systems/list-systems.d.ts.map +1 -1
- package/tools/systems/list-systems.js +14 -3
- package/tools/systems/list-systems.js.map +1 -1
- package/tools/systems/list-systems.mjs +14 -3
- package/tools/systems/list-systems.mjs.map +1 -1
- package/tools/systems/update-systems.d.mts +1 -1
- package/tools/systems/update-systems.d.mts.map +1 -1
- package/tools/systems/update-systems.d.ts +1 -1
- package/tools/systems/update-systems.d.ts.map +1 -1
- package/tools/systems/update-systems.js +11 -3
- package/tools/systems/update-systems.js.map +1 -1
- package/tools/systems/update-systems.mjs +11 -3
- package/tools/systems/update-systems.mjs.map +1 -1
- package/tools/systems/upsert-systems.d.mts +1 -1
- package/tools/systems/upsert-systems.d.mts.map +1 -1
- package/tools/systems/upsert-systems.d.ts +1 -1
- package/tools/systems/upsert-systems.d.ts.map +1 -1
- package/tools/systems/upsert-systems.js +12 -3
- package/tools/systems/upsert-systems.js.map +1 -1
- package/tools/systems/upsert-systems.mjs +12 -3
- package/tools/systems/upsert-systems.mjs.map +1 -1
- package/tools/systems/versions/get-systems-versions.d.mts +1 -1
- package/tools/systems/versions/get-systems-versions.d.mts.map +1 -1
- package/tools/systems/versions/get-systems-versions.d.ts +1 -1
- package/tools/systems/versions/get-systems-versions.d.ts.map +1 -1
- package/tools/systems/versions/get-systems-versions.js +13 -3
- package/tools/systems/versions/get-systems-versions.js.map +1 -1
- package/tools/systems/versions/get-systems-versions.mjs +13 -3
- package/tools/systems/versions/get-systems-versions.mjs.map +1 -1
- package/tools/systems/versions/upsert-systems-versions.d.mts +1 -1
- package/tools/systems/versions/upsert-systems-versions.d.mts.map +1 -1
- package/tools/systems/versions/upsert-systems-versions.d.ts +1 -1
- package/tools/systems/versions/upsert-systems-versions.d.ts.map +1 -1
- package/tools/systems/versions/upsert-systems-versions.js +12 -3
- package/tools/systems/versions/upsert-systems-versions.js.map +1 -1
- package/tools/systems/versions/upsert-systems-versions.mjs +12 -3
- package/tools/systems/versions/upsert-systems-versions.mjs.map +1 -1
- package/tools/testcases/create-testcases.d.mts +1 -1
- package/tools/testcases/create-testcases.d.mts.map +1 -1
- package/tools/testcases/create-testcases.d.ts +1 -1
- package/tools/testcases/create-testcases.d.ts.map +1 -1
- package/tools/testcases/create-testcases.js +12 -3
- package/tools/testcases/create-testcases.js.map +1 -1
- package/tools/testcases/create-testcases.mjs +12 -3
- package/tools/testcases/create-testcases.mjs.map +1 -1
- package/tools/testcases/delete-testcases.d.mts +1 -1
- package/tools/testcases/delete-testcases.d.mts.map +1 -1
- package/tools/testcases/delete-testcases.d.ts +1 -1
- package/tools/testcases/delete-testcases.d.ts.map +1 -1
- package/tools/testcases/delete-testcases.js +11 -3
- package/tools/testcases/delete-testcases.js.map +1 -1
- package/tools/testcases/delete-testcases.mjs +11 -3
- package/tools/testcases/delete-testcases.mjs.map +1 -1
- package/tools/testcases/get-testcases.d.mts +1 -1
- package/tools/testcases/get-testcases.d.mts.map +1 -1
- package/tools/testcases/get-testcases.d.ts +1 -1
- package/tools/testcases/get-testcases.d.ts.map +1 -1
- package/tools/testcases/get-testcases.js +13 -3
- package/tools/testcases/get-testcases.js.map +1 -1
- package/tools/testcases/get-testcases.mjs +13 -3
- package/tools/testcases/get-testcases.mjs.map +1 -1
- package/tools/testcases/list-testcases.d.mts +1 -1
- package/tools/testcases/list-testcases.d.mts.map +1 -1
- package/tools/testcases/list-testcases.d.ts +1 -1
- package/tools/testcases/list-testcases.d.ts.map +1 -1
- package/tools/testcases/list-testcases.js +14 -3
- package/tools/testcases/list-testcases.js.map +1 -1
- package/tools/testcases/list-testcases.mjs +14 -3
- package/tools/testcases/list-testcases.mjs.map +1 -1
- package/tools/testcases/update-testcases.d.mts +1 -1
- package/tools/testcases/update-testcases.d.mts.map +1 -1
- package/tools/testcases/update-testcases.d.ts +1 -1
- package/tools/testcases/update-testcases.d.ts.map +1 -1
- package/tools/testcases/update-testcases.js +14 -3
- package/tools/testcases/update-testcases.js.map +1 -1
- package/tools/testcases/update-testcases.mjs +14 -3
- package/tools/testcases/update-testcases.mjs.map +1 -1
- package/tools/testsets/create-testsets.d.mts +1 -1
- package/tools/testsets/create-testsets.d.mts.map +1 -1
- package/tools/testsets/create-testsets.d.ts +1 -1
- package/tools/testsets/create-testsets.d.ts.map +1 -1
- package/tools/testsets/create-testsets.js +12 -3
- package/tools/testsets/create-testsets.js.map +1 -1
- package/tools/testsets/create-testsets.mjs +12 -3
- package/tools/testsets/create-testsets.mjs.map +1 -1
- package/tools/testsets/delete-testsets.d.mts +1 -1
- package/tools/testsets/delete-testsets.d.mts.map +1 -1
- package/tools/testsets/delete-testsets.d.ts +1 -1
- package/tools/testsets/delete-testsets.d.ts.map +1 -1
- package/tools/testsets/delete-testsets.js +13 -3
- package/tools/testsets/delete-testsets.js.map +1 -1
- package/tools/testsets/delete-testsets.mjs +13 -3
- package/tools/testsets/delete-testsets.mjs.map +1 -1
- package/tools/testsets/get-testsets.d.mts +1 -1
- package/tools/testsets/get-testsets.d.mts.map +1 -1
- package/tools/testsets/get-testsets.d.ts +1 -1
- package/tools/testsets/get-testsets.d.ts.map +1 -1
- package/tools/testsets/get-testsets.js +13 -3
- package/tools/testsets/get-testsets.js.map +1 -1
- package/tools/testsets/get-testsets.mjs +13 -3
- package/tools/testsets/get-testsets.mjs.map +1 -1
- package/tools/testsets/list-testsets.d.mts +1 -1
- package/tools/testsets/list-testsets.d.mts.map +1 -1
- package/tools/testsets/list-testsets.d.ts +1 -1
- package/tools/testsets/list-testsets.d.ts.map +1 -1
- package/tools/testsets/list-testsets.js +14 -3
- package/tools/testsets/list-testsets.js.map +1 -1
- package/tools/testsets/list-testsets.mjs +14 -3
- package/tools/testsets/list-testsets.mjs.map +1 -1
- package/tools/testsets/update-testsets.d.mts +1 -1
- package/tools/testsets/update-testsets.d.mts.map +1 -1
- package/tools/testsets/update-testsets.d.ts +1 -1
- package/tools/testsets/update-testsets.d.ts.map +1 -1
- package/tools/testsets/update-testsets.js +12 -3
- package/tools/testsets/update-testsets.js.map +1 -1
- package/tools/testsets/update-testsets.mjs +12 -3
- package/tools/testsets/update-testsets.mjs.map +1 -1
- package/tools/types.d.mts +1 -1
- package/tools/types.d.mts.map +1 -1
- package/tools/types.d.ts +1 -1
- package/tools/types.d.ts.map +1 -1
- package/tools/types.js.map +1 -1
- package/tools/types.mjs.map +1 -1
package/server.mjs
CHANGED
|
@@ -1,79 +1,131 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
3
|
import { endpoints, query } from "./tools.mjs";
|
|
4
|
-
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
5
5
|
import Scorecard from 'scorecard-ai';
|
|
6
6
|
import { applyCompatibilityTransformations, defaultClientCapabilities, knownClients, parseEmbeddedJSON, } from "./compat.mjs";
|
|
7
7
|
import { dynamicTools } from "./dynamic-tools.mjs";
|
|
8
|
+
import { codeTool } from "./code-tool.mjs";
|
|
9
|
+
export { ClientType } from "./compat.mjs";
|
|
8
10
|
export { endpoints } from "./tools.mjs";
|
|
9
|
-
|
|
10
|
-
export const server = new McpServer({
|
|
11
|
+
export const newMcpServer = () => new McpServer({
|
|
11
12
|
name: 'scorecard_ai_api',
|
|
12
|
-
version: '2.
|
|
13
|
-
}, {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
},
|
|
17
|
-
});
|
|
13
|
+
version: '2.1.0',
|
|
14
|
+
}, { capabilities: { tools: {}, logging: {} } });
|
|
15
|
+
// Create server instance
|
|
16
|
+
export const server = newMcpServer();
|
|
18
17
|
/**
|
|
19
18
|
* Initializes the provided MCP Server with the given tools and handlers.
|
|
20
19
|
* If not provided, the default client, tools and handlers will be used.
|
|
21
20
|
*/
|
|
22
21
|
export function initMcpServer(params) {
|
|
23
|
-
const transformedEndpoints = selectTools(endpoints, params.mcpOptions);
|
|
24
|
-
const client = new Scorecard(params.clientOptions);
|
|
25
|
-
const capabilities = {
|
|
26
|
-
...defaultClientCapabilities,
|
|
27
|
-
...(params.mcpOptions.client ? knownClients[params.mcpOptions.client] : params.mcpOptions.capabilities),
|
|
28
|
-
};
|
|
29
|
-
init({ server: params.server, client, endpoints: transformedEndpoints, capabilities });
|
|
30
|
-
}
|
|
31
|
-
export function init(params) {
|
|
32
22
|
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
23
|
+
const mcpOptions = params.mcpOptions ?? {};
|
|
24
|
+
let providedEndpoints = null;
|
|
25
|
+
let endpointMap = null;
|
|
26
|
+
const initTools = async (implementation) => {
|
|
27
|
+
if (implementation && (!mcpOptions.client || mcpOptions.client === 'infer')) {
|
|
28
|
+
mcpOptions.client =
|
|
29
|
+
implementation.name.toLowerCase().includes('claude') ? 'claude'
|
|
30
|
+
: implementation.name.toLowerCase().includes('cursor') ? 'cursor'
|
|
31
|
+
: undefined;
|
|
32
|
+
mcpOptions.capabilities = {
|
|
33
|
+
...(mcpOptions.client && knownClients[mcpOptions.client]),
|
|
34
|
+
...mcpOptions.capabilities,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
providedEndpoints ?? (providedEndpoints = await selectTools(endpoints, mcpOptions));
|
|
38
|
+
endpointMap ?? (endpointMap = Object.fromEntries(providedEndpoints.map((endpoint) => [endpoint.tool.name, endpoint])));
|
|
39
|
+
};
|
|
40
|
+
const logAtLevel = (level) => (message, ...rest) => {
|
|
41
|
+
void server.sendLoggingMessage({
|
|
42
|
+
level,
|
|
43
|
+
data: { message, rest },
|
|
39
44
|
});
|
|
45
|
+
};
|
|
46
|
+
const logger = {
|
|
47
|
+
debug: logAtLevel('debug'),
|
|
48
|
+
info: logAtLevel('info'),
|
|
49
|
+
warn: logAtLevel('warning'),
|
|
50
|
+
error: logAtLevel('error'),
|
|
51
|
+
};
|
|
52
|
+
let client = new Scorecard({
|
|
53
|
+
...{ environment: (readEnv('SCORECARD_ENVIRONMENT') || undefined) },
|
|
54
|
+
logger,
|
|
55
|
+
...params.clientOptions,
|
|
56
|
+
defaultHeaders: {
|
|
57
|
+
...params.clientOptions?.defaultHeaders,
|
|
58
|
+
'X-Stainless-MCP': 'true',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
40
61
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
62
|
+
if (providedEndpoints === null) {
|
|
63
|
+
await initTools(server.getClientVersion());
|
|
64
|
+
}
|
|
41
65
|
return {
|
|
42
66
|
tools: providedEndpoints.map((endpoint) => endpoint.tool),
|
|
43
67
|
};
|
|
44
68
|
});
|
|
45
69
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
70
|
+
if (endpointMap === null) {
|
|
71
|
+
await initTools(server.getClientVersion());
|
|
72
|
+
}
|
|
46
73
|
const { name, arguments: args } = request.params;
|
|
47
74
|
const endpoint = endpointMap[name];
|
|
48
75
|
if (!endpoint) {
|
|
49
76
|
throw new Error(`Unknown tool: ${name}`);
|
|
50
77
|
}
|
|
51
|
-
return executeHandler(endpoint.tool, endpoint.handler, client, args,
|
|
78
|
+
return executeHandler(endpoint.tool, endpoint.handler, client, args, mcpOptions.capabilities);
|
|
79
|
+
});
|
|
80
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
81
|
+
const { level } = request.params;
|
|
82
|
+
switch (level) {
|
|
83
|
+
case 'debug':
|
|
84
|
+
client = client.withOptions({ logLevel: 'debug' });
|
|
85
|
+
break;
|
|
86
|
+
case 'info':
|
|
87
|
+
client = client.withOptions({ logLevel: 'info' });
|
|
88
|
+
break;
|
|
89
|
+
case 'notice':
|
|
90
|
+
case 'warning':
|
|
91
|
+
client = client.withOptions({ logLevel: 'warn' });
|
|
92
|
+
break;
|
|
93
|
+
case 'error':
|
|
94
|
+
client = client.withOptions({ logLevel: 'error' });
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
client = client.withOptions({ logLevel: 'off' });
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return {};
|
|
52
101
|
});
|
|
53
102
|
}
|
|
54
103
|
/**
|
|
55
104
|
* Selects the tools to include in the MCP Server based on the provided options.
|
|
56
105
|
*/
|
|
57
|
-
export function selectTools(endpoints, options) {
|
|
58
|
-
const filteredEndpoints = query(options
|
|
106
|
+
export async function selectTools(endpoints, options) {
|
|
107
|
+
const filteredEndpoints = query(options?.filters ?? [], endpoints);
|
|
59
108
|
let includedTools = filteredEndpoints;
|
|
60
109
|
if (includedTools.length > 0) {
|
|
61
|
-
if (options
|
|
110
|
+
if (options?.includeDynamicTools) {
|
|
62
111
|
includedTools = dynamicTools(includedTools);
|
|
63
112
|
}
|
|
64
113
|
}
|
|
65
114
|
else {
|
|
66
|
-
if (options
|
|
115
|
+
if (options?.includeAllTools) {
|
|
67
116
|
includedTools = endpoints;
|
|
68
117
|
}
|
|
69
|
-
else if (options
|
|
118
|
+
else if (options?.includeDynamicTools) {
|
|
70
119
|
includedTools = dynamicTools(endpoints);
|
|
71
120
|
}
|
|
121
|
+
else if (options?.includeCodeTools) {
|
|
122
|
+
includedTools = [await codeTool()];
|
|
123
|
+
}
|
|
72
124
|
else {
|
|
73
125
|
includedTools = endpoints;
|
|
74
126
|
}
|
|
75
127
|
}
|
|
76
|
-
const capabilities = { ...defaultClientCapabilities, ...options
|
|
128
|
+
const capabilities = { ...defaultClientCapabilities, ...options?.capabilities };
|
|
77
129
|
return applyCompatibilityTransformations(includedTools, capabilities);
|
|
78
130
|
}
|
|
79
131
|
/**
|
|
@@ -81,7 +133,7 @@ export function selectTools(endpoints, options) {
|
|
|
81
133
|
*/
|
|
82
134
|
export async function executeHandler(tool, handler, client, args, compatibilityOptions) {
|
|
83
135
|
const options = { ...defaultClientCapabilities, ...compatibilityOptions };
|
|
84
|
-
if (options.validJson && args) {
|
|
136
|
+
if (!options.validJson && args) {
|
|
85
137
|
args = parseEmbeddedJSON(args, tool.inputSchema);
|
|
86
138
|
}
|
|
87
139
|
return await handler(client, args || {});
|
package/server.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.mjs","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAG/E,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAC5D,EAAY,SAAS,EAAmB,KAAK,EAAE;OAC/C,
|
|
1
|
+
{"version":3,"file":"server.mjs","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAG/E,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAC5D,EAAY,SAAS,EAAmB,KAAK,EAAE;OAC/C,EACL,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,GAGtB,MAAM,oCAAoC;OAEpC,SAAS,MAAM,cAAc;OAC7B,EACL,iCAAiC,EAEjC,yBAAyB,EACzB,YAAY,EACZ,iBAAiB,GAClB;OACM,EAAE,YAAY,EAAE;OAChB,EAAE,QAAQ,EAAE;OAIZ,EAAE,UAAU,EAAE;OAGd,EAAE,SAAS,EAAE;AAEpB,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE,CAC/B,IAAI,SAAS,CACX;IACE,IAAI,EAAE,kBAAkB;IACxB,OAAO,EAAE,OAAO;CACjB,EACD,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAC7C,CA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AAI,EAAE,CAAC;QAC/B,IAAI,GAAG,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IACnD,CAAC;IACD,OAAO,MAAM,OAAO,CAAC,MAAM,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAAW,EAAsB,EAAE;IACzD,IAAI,OAAQ,UAAkB,CAAC,OAAO,KAAK,WAAW,EAAE,CAAC;QACvD,OAAQ,UAAkB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;IACxD,CAAC;SAAM,IAAI,OAAQ,UAAkB,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QAC3D,OAAQ,UAAkB,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;IAC1D,CAAC;IACD,OAAO;AACT,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAW,EAAU,EAAE;IACpD,IAAI,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,aAAa,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { ClientOptions } from 'scorecard-ai';
|
|
4
|
+
|
|
5
|
+
export type WorkerInput = {
|
|
6
|
+
opts: ClientOptions;
|
|
7
|
+
code: string;
|
|
8
|
+
};
|
|
9
|
+
export type WorkerSuccess = {
|
|
10
|
+
result: unknown | null;
|
|
11
|
+
logLines: string[];
|
|
12
|
+
errLines: string[];
|
|
13
|
+
};
|
|
14
|
+
export type WorkerError = { message: string | undefined };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import util from 'node:util';
|
|
4
|
+
import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
|
|
5
|
+
import { Scorecard } from 'scorecard-ai';
|
|
6
|
+
|
|
7
|
+
const fetch = async (req: Request): Promise<Response> => {
|
|
8
|
+
const { opts, code } = (await req.json()) as WorkerInput;
|
|
9
|
+
const client = new Scorecard({
|
|
10
|
+
...opts,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const logLines: string[] = [];
|
|
14
|
+
const errLines: string[] = [];
|
|
15
|
+
const console = {
|
|
16
|
+
log: (...args: unknown[]) => {
|
|
17
|
+
logLines.push(util.format(...args));
|
|
18
|
+
},
|
|
19
|
+
error: (...args: unknown[]) => {
|
|
20
|
+
errLines.push(util.format(...args));
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
try {
|
|
24
|
+
let run_ = async (client: any) => {};
|
|
25
|
+
eval(`
|
|
26
|
+
${code}
|
|
27
|
+
run_ = run;
|
|
28
|
+
`);
|
|
29
|
+
const result = await run_(client);
|
|
30
|
+
return Response.json({
|
|
31
|
+
result,
|
|
32
|
+
logLines,
|
|
33
|
+
errLines,
|
|
34
|
+
} satisfies WorkerSuccess);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
const message = e instanceof Error ? e.message : undefined;
|
|
37
|
+
return Response.json(
|
|
38
|
+
{
|
|
39
|
+
message,
|
|
40
|
+
} satisfies WorkerError,
|
|
41
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default { fetch };
|
package/src/code-tool.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import Scorecard, { ClientOptions } from 'scorecard-ai';
|
|
6
|
+
import { Endpoint, ContentBlock, Metadata } from './tools/types';
|
|
7
|
+
|
|
8
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
|
|
10
|
+
import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A tool that runs code against a copy of the SDK.
|
|
14
|
+
*
|
|
15
|
+
* Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
|
|
16
|
+
* we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
|
|
17
|
+
* a generic endpoint that can be used to invoke any endpoint with the provided arguments.
|
|
18
|
+
*
|
|
19
|
+
* @param endpoints - The endpoints to include in the list.
|
|
20
|
+
*/
|
|
21
|
+
export async function codeTool(): Promise<Endpoint> {
|
|
22
|
+
const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
|
|
23
|
+
const tool: Tool = {
|
|
24
|
+
name: 'execute',
|
|
25
|
+
description:
|
|
26
|
+
'Runs Typescript code to interact with the API.\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client, and it will be run.\nDo not initialize a client, but instead use the client that you are given as a parameter.\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
|
|
27
|
+
inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Import dynamically to avoid failing at import time in cases where the environment is not well-supported.
|
|
31
|
+
const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
|
|
32
|
+
const { workerPath } = await import('./code-tool-paths.cjs');
|
|
33
|
+
|
|
34
|
+
const handler = async (client: Scorecard, args: unknown) => {
|
|
35
|
+
const baseURLHostname = new URL(client.baseURL).hostname;
|
|
36
|
+
const { code } = args as { code: string };
|
|
37
|
+
|
|
38
|
+
const worker = await newDenoHTTPWorker(pathToFileURL(workerPath), {
|
|
39
|
+
runFlags: [
|
|
40
|
+
`--node-modules-dir=manual`,
|
|
41
|
+
`--allow-read=code-tool-worker.mjs,${workerPath.replace(/([\/\\]node_modules)[\/\\].+$/, '$1')}/`,
|
|
42
|
+
`--allow-net=${baseURLHostname}`,
|
|
43
|
+
// Allow environment variables because instantiating the client will try to read from them,
|
|
44
|
+
// even though they are not set.
|
|
45
|
+
'--allow-env',
|
|
46
|
+
],
|
|
47
|
+
printOutput: true,
|
|
48
|
+
spawnOptions: {
|
|
49
|
+
cwd: dirname(workerPath),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const resp = await new Promise<Response>((resolve, reject) => {
|
|
55
|
+
worker.addEventListener('exit', (exitCode) => {
|
|
56
|
+
reject(new Error(`Worker exited with code ${exitCode}`));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const opts: ClientOptions = {
|
|
60
|
+
baseURL: client.baseURL,
|
|
61
|
+
apiKey: client.apiKey,
|
|
62
|
+
defaultHeaders: {
|
|
63
|
+
'X-Stainless-MCP': 'true',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const req = worker.request(
|
|
68
|
+
'http://localhost',
|
|
69
|
+
{
|
|
70
|
+
headers: {
|
|
71
|
+
'content-type': 'application/json',
|
|
72
|
+
},
|
|
73
|
+
method: 'POST',
|
|
74
|
+
},
|
|
75
|
+
(resp) => {
|
|
76
|
+
const body: Uint8Array[] = [];
|
|
77
|
+
resp.on('error', (err) => {
|
|
78
|
+
reject(err);
|
|
79
|
+
});
|
|
80
|
+
resp.on('data', (chunk) => {
|
|
81
|
+
body.push(chunk);
|
|
82
|
+
});
|
|
83
|
+
resp.on('end', () => {
|
|
84
|
+
resolve(
|
|
85
|
+
new Response(Buffer.concat(body).toString(), {
|
|
86
|
+
status: resp.statusCode ?? 200,
|
|
87
|
+
headers: resp.headers as any,
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const body = JSON.stringify({
|
|
95
|
+
opts,
|
|
96
|
+
code,
|
|
97
|
+
} satisfies WorkerInput);
|
|
98
|
+
|
|
99
|
+
req.write(body, (err) => {
|
|
100
|
+
if (err !== null && err !== undefined) {
|
|
101
|
+
reject(err);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
req.end();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (resp.status === 200) {
|
|
109
|
+
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
|
|
110
|
+
const returnOutput: ContentBlock | null =
|
|
111
|
+
result === null ? null
|
|
112
|
+
: result === undefined ? null
|
|
113
|
+
: {
|
|
114
|
+
type: 'text',
|
|
115
|
+
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
|
|
116
|
+
};
|
|
117
|
+
const logOutput: ContentBlock | null =
|
|
118
|
+
logLines.length === 0 ?
|
|
119
|
+
null
|
|
120
|
+
: {
|
|
121
|
+
type: 'text',
|
|
122
|
+
text: logLines.join('\n'),
|
|
123
|
+
};
|
|
124
|
+
const errOutput: ContentBlock | null =
|
|
125
|
+
errLines.length === 0 ?
|
|
126
|
+
null
|
|
127
|
+
: {
|
|
128
|
+
type: 'text',
|
|
129
|
+
text: 'Error output:\n' + errLines.join('\n'),
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
|
|
133
|
+
};
|
|
134
|
+
} else {
|
|
135
|
+
const { message } = (await resp.json()) as WorkerError;
|
|
136
|
+
throw new Error(message);
|
|
137
|
+
}
|
|
138
|
+
} catch (e) {
|
|
139
|
+
throw e;
|
|
140
|
+
} finally {
|
|
141
|
+
worker.terminate();
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
return { metadata, tool, handler };
|
|
146
|
+
}
|
package/src/compat.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
import { Endpoint } from './tools';
|
|
3
4
|
|
|
4
5
|
export interface ClientCapabilities {
|
|
@@ -19,12 +20,13 @@ export const defaultClientCapabilities: ClientCapabilities = {
|
|
|
19
20
|
toolNameLength: undefined,
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
+
export const ClientType = z.enum(['openai-agents', 'claude', 'claude-code', 'cursor', 'infer']);
|
|
24
|
+
export type ClientType = z.infer<typeof ClientType>;
|
|
23
25
|
|
|
24
26
|
// Client presets for compatibility
|
|
25
27
|
// Note that these could change over time as models get better, so this is
|
|
26
28
|
// a best effort.
|
|
27
|
-
export const knownClients: Record<ClientType, ClientCapabilities> = {
|
|
29
|
+
export const knownClients: Record<Exclude<ClientType, 'infer'>, ClientCapabilities> = {
|
|
28
30
|
'openai-agents': {
|
|
29
31
|
topLevelUnions: false,
|
|
30
32
|
validJson: true,
|
|
@@ -70,8 +72,11 @@ export function parseEmbeddedJSON(args: Record<string, unknown>, schema: Record<
|
|
|
70
72
|
if (typeof value === 'string') {
|
|
71
73
|
try {
|
|
72
74
|
const parsed = JSON.parse(value);
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
// Only parse if result is a plain object (not array, null, or primitive)
|
|
76
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
77
|
+
newArgs[key] = parsed;
|
|
78
|
+
updated = true;
|
|
79
|
+
}
|
|
75
80
|
} catch (e) {
|
|
76
81
|
// Not valid JSON, leave as is
|
|
77
82
|
}
|
package/src/filtering.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import initJq from 'jq-web';
|
|
3
|
+
|
|
4
|
+
export async function maybeFilter(jqFilter: unknown | undefined, response: any): Promise<any> {
|
|
5
|
+
if (jqFilter && typeof jqFilter === 'string') {
|
|
6
|
+
return await jq(response, jqFilter);
|
|
7
|
+
} else {
|
|
8
|
+
return response;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function jq(json: any, jqFilter: string) {
|
|
13
|
+
return (await initJq).json(json, jqFilter);
|
|
14
|
+
}
|
package/src/headers.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { IncomingMessage } from 'node:http';
|
|
4
|
+
import { ClientOptions } from 'scorecard-ai';
|
|
5
|
+
|
|
6
|
+
export const parseAuthHeaders = (req: IncomingMessage): Partial<ClientOptions> => {
|
|
7
|
+
if (req.headers.authorization) {
|
|
8
|
+
const scheme = req.headers.authorization.split(' ')[0]!;
|
|
9
|
+
const value = req.headers.authorization.slice(scheme.length + 1);
|
|
10
|
+
switch (scheme) {
|
|
11
|
+
case 'Bearer':
|
|
12
|
+
return { apiKey: req.headers.authorization.slice('Bearer '.length) };
|
|
13
|
+
default:
|
|
14
|
+
throw new Error(`Unsupported authorization scheme`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const apiKey =
|
|
19
|
+
Array.isArray(req.headers['x-scorecard-api-key']) ?
|
|
20
|
+
req.headers['x-scorecard-api-key'][0]
|
|
21
|
+
: req.headers['x-scorecard-api-key'];
|
|
22
|
+
return { apiKey };
|
|
23
|
+
};
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
4
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
5
|
+
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import { fromError } from 'zod-validation-error/v3';
|
|
8
|
+
import { McpOptions, parseQueryOptions } from './options';
|
|
9
|
+
import { ClientOptions, initMcpServer, newMcpServer } from './server';
|
|
10
|
+
import { parseAuthHeaders } from './headers';
|
|
11
|
+
|
|
12
|
+
const newServer = ({
|
|
13
|
+
clientOptions,
|
|
14
|
+
mcpOptions: defaultMcpOptions,
|
|
15
|
+
req,
|
|
16
|
+
res,
|
|
17
|
+
}: {
|
|
18
|
+
clientOptions: ClientOptions;
|
|
19
|
+
mcpOptions: McpOptions;
|
|
20
|
+
req: express.Request;
|
|
21
|
+
res: express.Response;
|
|
22
|
+
}): McpServer | null => {
|
|
23
|
+
const server = newMcpServer();
|
|
24
|
+
|
|
25
|
+
let mcpOptions: McpOptions;
|
|
26
|
+
try {
|
|
27
|
+
mcpOptions = parseQueryOptions(defaultMcpOptions, req.query);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
res.status(400).json({
|
|
30
|
+
jsonrpc: '2.0',
|
|
31
|
+
error: {
|
|
32
|
+
code: -32000,
|
|
33
|
+
message: `Invalid request: ${fromError(error)}`,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const authOptions = parseAuthHeaders(req);
|
|
41
|
+
initMcpServer({
|
|
42
|
+
server: server,
|
|
43
|
+
clientOptions: {
|
|
44
|
+
...clientOptions,
|
|
45
|
+
...authOptions,
|
|
46
|
+
},
|
|
47
|
+
mcpOptions,
|
|
48
|
+
});
|
|
49
|
+
} catch {
|
|
50
|
+
res.status(401).json({
|
|
51
|
+
jsonrpc: '2.0',
|
|
52
|
+
error: {
|
|
53
|
+
code: -32000,
|
|
54
|
+
message: 'Unauthorized',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return server;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const post =
|
|
64
|
+
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
|
|
65
|
+
async (req: express.Request, res: express.Response) => {
|
|
66
|
+
const server = newServer({ ...options, req, res });
|
|
67
|
+
// If we return null, we already set the authorization error.
|
|
68
|
+
if (server === null) return;
|
|
69
|
+
const transport = new StreamableHTTPServerTransport({
|
|
70
|
+
// Stateless server
|
|
71
|
+
sessionIdGenerator: undefined,
|
|
72
|
+
});
|
|
73
|
+
await server.connect(transport);
|
|
74
|
+
await transport.handleRequest(req, res, req.body);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const get = async (req: express.Request, res: express.Response) => {
|
|
78
|
+
res.status(405).json({
|
|
79
|
+
jsonrpc: '2.0',
|
|
80
|
+
error: {
|
|
81
|
+
code: -32000,
|
|
82
|
+
message: 'Method not supported',
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const del = async (req: express.Request, res: express.Response) => {
|
|
88
|
+
res.status(405).json({
|
|
89
|
+
jsonrpc: '2.0',
|
|
90
|
+
error: {
|
|
91
|
+
code: -32000,
|
|
92
|
+
message: 'Method not supported',
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const streamableHTTPApp = ({
|
|
98
|
+
clientOptions = {},
|
|
99
|
+
mcpOptions = {},
|
|
100
|
+
}: {
|
|
101
|
+
clientOptions?: ClientOptions;
|
|
102
|
+
mcpOptions?: McpOptions;
|
|
103
|
+
}): express.Express => {
|
|
104
|
+
const app = express();
|
|
105
|
+
app.set('query parser', 'extended');
|
|
106
|
+
app.use(express.json());
|
|
107
|
+
|
|
108
|
+
app.get('/', get);
|
|
109
|
+
app.post('/', post({ clientOptions, mcpOptions }));
|
|
110
|
+
app.delete('/', del);
|
|
111
|
+
|
|
112
|
+
return app;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const launchStreamableHTTPServer = async (options: McpOptions, port: number | string | undefined) => {
|
|
116
|
+
const app = streamableHTTPApp({ mcpOptions: options });
|
|
117
|
+
const server = app.listen(port);
|
|
118
|
+
const address = server.address();
|
|
119
|
+
|
|
120
|
+
if (typeof address === 'string') {
|
|
121
|
+
console.error(`MCP Server running on streamable HTTP at ${address}`);
|
|
122
|
+
} else if (address !== null) {
|
|
123
|
+
console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
|
|
124
|
+
} else {
|
|
125
|
+
console.error(`MCP Server running on streamable HTTP on port ${port}`);
|
|
126
|
+
}
|
|
127
|
+
};
|