nimble-js-mcp 0.5.1
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 +201 -0
- package/README.md +101 -0
- package/auth.d.mts +6 -0
- package/auth.d.mts.map +1 -0
- package/auth.d.ts +6 -0
- package/auth.d.ts.map +1 -0
- package/auth.js +37 -0
- package/auth.js.map +1 -0
- package/auth.mjs +32 -0
- package/auth.mjs.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.d.mts +15 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +15 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +108 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +105 -0
- package/code-tool.mjs.map +1 -0
- package/docs-search-tool.d.mts +57 -0
- package/docs-search-tool.d.mts.map +1 -0
- package/docs-search-tool.d.ts +57 -0
- package/docs-search-tool.d.ts.map +1 -0
- package/docs-search-tool.js +55 -0
- package/docs-search-tool.js.map +1 -0
- package/docs-search-tool.mjs +51 -0
- package/docs-search-tool.mjs.map +1 -0
- package/http.d.mts +14 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +14 -0
- package/http.d.ts.map +1 -0
- package/http.js +95 -0
- package/http.js.map +1 -0
- package/http.mjs +87 -0
- package/http.mjs.map +1 -0
- package/index.d.mts +3 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -0
- package/index.js +59 -0
- package/index.js.map +1 -0
- package/index.mjs +57 -0
- package/index.mjs.map +1 -0
- package/instructions.d.mts +2 -0
- package/instructions.d.mts.map +1 -0
- package/instructions.d.ts +2 -0
- package/instructions.d.ts.map +1 -0
- package/instructions.js +55 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +52 -0
- package/instructions.mjs.map +1 -0
- package/methods.d.mts +10 -0
- package/methods.d.mts.map +1 -0
- package/methods.d.ts +10 -0
- package/methods.d.ts.map +1 -0
- package/methods.js +155 -0
- package/methods.js.map +1 -0
- package/methods.mjs +151 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +16 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +16 -0
- package/options.d.ts.map +1 -0
- package/options.js +99 -0
- package/options.js.map +1 -0
- package/options.mjs +92 -0
- package/options.mjs.map +1 -0
- package/package.json +194 -0
- package/server.d.mts +29 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +29 -0
- package/server.d.ts.map +1 -0
- package/server.js +156 -0
- package/server.js.map +1 -0
- package/server.mjs +146 -0
- package/server.mjs.map +1 -0
- package/src/auth.ts +42 -0
- package/src/code-tool-types.ts +16 -0
- package/src/code-tool.ts +136 -0
- package/src/docs-search-tool.ts +68 -0
- package/src/http.ts +127 -0
- package/src/index.ts +65 -0
- package/src/instructions.ts +74 -0
- package/src/methods.ts +176 -0
- package/src/options.ts +129 -0
- package/src/server.ts +183 -0
- package/src/stdio.ts +13 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +123 -0
- package/src/util.ts +25 -0
- 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/types.d.mts +59 -0
- package/types.d.mts.map +1 -0
- package/types.d.ts +59 -0
- package/types.d.ts.map +1 -0
- package/types.js +58 -0
- package/types.js.map +1 -0
- package/types.mjs +53 -0
- package/types.mjs.map +1 -0
- package/util.d.mts +4 -0
- package/util.d.mts.map +1 -0
- package/util.d.ts +4 -0
- package/util.d.ts.map +1 -0
- package/util.js +30 -0
- package/util.js.map +1 -0
- package/util.mjs +24 -0
- package/util.mjs.map +1 -0
package/src/methods.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { McpOptions } from './options';
|
|
4
|
+
|
|
5
|
+
export type SdkMethod = {
|
|
6
|
+
clientCallName: string;
|
|
7
|
+
fullyQualifiedName: string;
|
|
8
|
+
httpMethod?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'query';
|
|
9
|
+
httpPath?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const sdkMethods: SdkMethod[] = [
|
|
13
|
+
{
|
|
14
|
+
clientCallName: 'client.extract',
|
|
15
|
+
fullyQualifiedName: 'extract',
|
|
16
|
+
httpMethod: 'post',
|
|
17
|
+
httpPath: '/v1/extract',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
clientCallName: 'client.extractAsync',
|
|
21
|
+
fullyQualifiedName: 'extractAsync',
|
|
22
|
+
httpMethod: 'post',
|
|
23
|
+
httpPath: '/v1/extract/async',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
clientCallName: 'client.map',
|
|
27
|
+
fullyQualifiedName: 'map',
|
|
28
|
+
httpMethod: 'post',
|
|
29
|
+
httpPath: '/v1/map',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
clientCallName: 'client.search',
|
|
33
|
+
fullyQualifiedName: 'search',
|
|
34
|
+
httpMethod: 'post',
|
|
35
|
+
httpPath: '/v1/search',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
clientCallName: 'client.agent.list',
|
|
39
|
+
fullyQualifiedName: 'agent.list',
|
|
40
|
+
httpMethod: 'get',
|
|
41
|
+
httpPath: '/v1/agents',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
clientCallName: 'client.agent.get',
|
|
45
|
+
fullyQualifiedName: 'agent.get',
|
|
46
|
+
httpMethod: 'get',
|
|
47
|
+
httpPath: '/v1/agents/{template_name}',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
clientCallName: 'client.agent.run',
|
|
51
|
+
fullyQualifiedName: 'agent.run',
|
|
52
|
+
httpMethod: 'post',
|
|
53
|
+
httpPath: '/v1/agents/run',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
clientCallName: 'client.agent.runAsync',
|
|
57
|
+
fullyQualifiedName: 'agent.runAsync',
|
|
58
|
+
httpMethod: 'post',
|
|
59
|
+
httpPath: '/v1/agents/async',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
clientCallName: 'client.crawl.list',
|
|
63
|
+
fullyQualifiedName: 'crawl.list',
|
|
64
|
+
httpMethod: 'get',
|
|
65
|
+
httpPath: '/v1/crawl',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
clientCallName: 'client.crawl.run',
|
|
69
|
+
fullyQualifiedName: 'crawl.run',
|
|
70
|
+
httpMethod: 'post',
|
|
71
|
+
httpPath: '/v1/crawl',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
clientCallName: 'client.crawl.status',
|
|
75
|
+
fullyQualifiedName: 'crawl.status',
|
|
76
|
+
httpMethod: 'get',
|
|
77
|
+
httpPath: '/v1/crawl/{id}',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
clientCallName: 'client.crawl.terminate',
|
|
81
|
+
fullyQualifiedName: 'crawl.terminate',
|
|
82
|
+
httpMethod: 'delete',
|
|
83
|
+
httpPath: '/v1/crawl/{id}',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
clientCallName: 'client.tasks.list',
|
|
87
|
+
fullyQualifiedName: 'tasks.list',
|
|
88
|
+
httpMethod: 'get',
|
|
89
|
+
httpPath: '/v1/tasks',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
clientCallName: 'client.tasks.get',
|
|
93
|
+
fullyQualifiedName: 'tasks.get',
|
|
94
|
+
httpMethod: 'get',
|
|
95
|
+
httpPath: '/v1/tasks/{task_id}',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
clientCallName: 'client.tasks.results',
|
|
99
|
+
fullyQualifiedName: 'tasks.results',
|
|
100
|
+
httpMethod: 'get',
|
|
101
|
+
httpPath: '/v1/tasks/{task_id}/results',
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
function allowedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
106
|
+
if (!options) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let allowedMethods: SdkMethod[];
|
|
111
|
+
|
|
112
|
+
if (options.codeAllowHttpGets || options.codeAllowedMethods) {
|
|
113
|
+
// Start with nothing allowed and then add into it from options
|
|
114
|
+
let allowedMethodsSet = new Set<SdkMethod>();
|
|
115
|
+
|
|
116
|
+
if (options.codeAllowHttpGets) {
|
|
117
|
+
// Add all methods that map to an HTTP GET
|
|
118
|
+
sdkMethods
|
|
119
|
+
.filter((method) => method.httpMethod === 'get')
|
|
120
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (options.codeAllowedMethods) {
|
|
124
|
+
// Add all methods that match any of the allowed regexps
|
|
125
|
+
const allowedRegexps = options.codeAllowedMethods.map((pattern) => {
|
|
126
|
+
try {
|
|
127
|
+
return new RegExp(pattern);
|
|
128
|
+
} catch (e) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Invalid regex pattern for allowed method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
sdkMethods
|
|
136
|
+
.filter((method) => allowedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)))
|
|
137
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
allowedMethods = Array.from(allowedMethodsSet);
|
|
141
|
+
} else {
|
|
142
|
+
// Start with everything allowed
|
|
143
|
+
allowedMethods = [...sdkMethods];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (options.codeBlockedMethods) {
|
|
147
|
+
// Filter down based on blocked regexps
|
|
148
|
+
const blockedRegexps = options.codeBlockedMethods.map((pattern) => {
|
|
149
|
+
try {
|
|
150
|
+
return new RegExp(pattern);
|
|
151
|
+
} catch (e) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Invalid regex pattern for blocked method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
allowedMethods = allowedMethods.filter(
|
|
159
|
+
(method) => !blockedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return allowedMethods;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function blockedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
167
|
+
const allowedMethods = allowedMethodsForCodeTool(options);
|
|
168
|
+
if (!allowedMethods) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const allowedSet = new Set(allowedMethods.map((method) => method.fullyQualifiedName));
|
|
173
|
+
|
|
174
|
+
// Return any methods that are not explicitly allowed
|
|
175
|
+
return sdkMethods.filter((method) => !allowedSet.has(method.fullyQualifiedName));
|
|
176
|
+
}
|
package/src/options.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import qs from 'qs';
|
|
4
|
+
import yargs from 'yargs';
|
|
5
|
+
import { hideBin } from 'yargs/helpers';
|
|
6
|
+
import z from 'zod';
|
|
7
|
+
import { readEnv } from './util';
|
|
8
|
+
|
|
9
|
+
export type CLIOptions = McpOptions & {
|
|
10
|
+
debug: boolean;
|
|
11
|
+
transport: 'stdio' | 'http';
|
|
12
|
+
port: number | undefined;
|
|
13
|
+
socket: string | undefined;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type McpOptions = {
|
|
17
|
+
includeDocsTools?: boolean | undefined;
|
|
18
|
+
stainlessApiKey?: string | undefined;
|
|
19
|
+
codeAllowHttpGets?: boolean | undefined;
|
|
20
|
+
codeAllowedMethods?: string[] | undefined;
|
|
21
|
+
codeBlockedMethods?: string[] | undefined;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function parseCLIOptions(): CLIOptions {
|
|
25
|
+
const opts = yargs(hideBin(process.argv))
|
|
26
|
+
.option('code-allow-http-gets', {
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
description:
|
|
29
|
+
'Allow all code tool methods that map to HTTP GET operations. If all code-allow-* flags are unset, then everything is allowed.',
|
|
30
|
+
})
|
|
31
|
+
.option('code-allowed-methods', {
|
|
32
|
+
type: 'string',
|
|
33
|
+
array: true,
|
|
34
|
+
description:
|
|
35
|
+
'Methods to explicitly allow for code tool. Evaluated as regular expressions against method fully qualified names. If all code-allow-* flags are unset, then everything is allowed.',
|
|
36
|
+
})
|
|
37
|
+
.option('code-blocked-methods', {
|
|
38
|
+
type: 'string',
|
|
39
|
+
array: true,
|
|
40
|
+
description:
|
|
41
|
+
'Methods to explicitly block for code tool. Evaluated as regular expressions against method fully qualified names. If all code-allow-* flags are unset, then everything is allowed.',
|
|
42
|
+
})
|
|
43
|
+
.option('debug', { type: 'boolean', description: 'Enable debug logging' })
|
|
44
|
+
.option('no-tools', {
|
|
45
|
+
type: 'string',
|
|
46
|
+
array: true,
|
|
47
|
+
choices: ['code', 'docs'],
|
|
48
|
+
description: 'Tools to explicitly disable',
|
|
49
|
+
})
|
|
50
|
+
.option('port', {
|
|
51
|
+
type: 'number',
|
|
52
|
+
default: 3000,
|
|
53
|
+
description: 'Port to serve on if using http transport',
|
|
54
|
+
})
|
|
55
|
+
.option('socket', { type: 'string', description: 'Unix socket to serve on if using http transport' })
|
|
56
|
+
.option('stainless-api-key', {
|
|
57
|
+
type: 'string',
|
|
58
|
+
default: readEnv('STAINLESS_API_KEY'),
|
|
59
|
+
description:
|
|
60
|
+
'API key for Stainless. Used to authenticate requests to Stainless-hosted tools endpoints.',
|
|
61
|
+
})
|
|
62
|
+
.option('tools', {
|
|
63
|
+
type: 'string',
|
|
64
|
+
array: true,
|
|
65
|
+
choices: ['code', 'docs'],
|
|
66
|
+
description: 'Tools to explicitly enable',
|
|
67
|
+
})
|
|
68
|
+
.option('transport', {
|
|
69
|
+
type: 'string',
|
|
70
|
+
choices: ['stdio', 'http'],
|
|
71
|
+
default: 'stdio',
|
|
72
|
+
description: 'What transport to use; stdio for local servers or http for remote servers',
|
|
73
|
+
})
|
|
74
|
+
.env('MCP_SERVER')
|
|
75
|
+
.version(true)
|
|
76
|
+
.help();
|
|
77
|
+
|
|
78
|
+
const argv = opts.parseSync();
|
|
79
|
+
|
|
80
|
+
const shouldIncludeToolType = (toolType: 'code' | 'docs') =>
|
|
81
|
+
argv.noTools?.includes(toolType) ? false
|
|
82
|
+
: argv.tools?.includes(toolType) ? true
|
|
83
|
+
: undefined;
|
|
84
|
+
|
|
85
|
+
const includeDocsTools = shouldIncludeToolType('docs');
|
|
86
|
+
|
|
87
|
+
const transport = argv.transport as 'stdio' | 'http';
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
...(includeDocsTools !== undefined && { includeDocsTools }),
|
|
91
|
+
debug: !!argv.debug,
|
|
92
|
+
stainlessApiKey: argv.stainlessApiKey,
|
|
93
|
+
codeAllowHttpGets: argv.codeAllowHttpGets,
|
|
94
|
+
codeAllowedMethods: argv.codeAllowedMethods,
|
|
95
|
+
codeBlockedMethods: argv.codeBlockedMethods,
|
|
96
|
+
transport,
|
|
97
|
+
port: argv.port,
|
|
98
|
+
socket: argv.socket,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
|
|
103
|
+
z.preprocess(
|
|
104
|
+
(val) =>
|
|
105
|
+
Array.isArray(val) ? val
|
|
106
|
+
: val ? [val]
|
|
107
|
+
: val,
|
|
108
|
+
z.array(zodType).optional(),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const QueryOptions = z.object({
|
|
112
|
+
tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to use'),
|
|
113
|
+
no_tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to not use.'),
|
|
114
|
+
tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): McpOptions {
|
|
118
|
+
const queryObject = typeof query === 'string' ? qs.parse(query) : query;
|
|
119
|
+
const queryOptions = QueryOptions.parse(queryObject);
|
|
120
|
+
|
|
121
|
+
let docsTools: boolean | undefined =
|
|
122
|
+
queryOptions.no_tools && queryOptions.no_tools?.includes('docs') ? false
|
|
123
|
+
: queryOptions.tools?.includes('docs') ? true
|
|
124
|
+
: defaultOptions.includeDocsTools;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...(docsTools !== undefined && { includeDocsTools: docsTools }),
|
|
128
|
+
};
|
|
129
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema,
|
|
8
|
+
SetLevelRequestSchema,
|
|
9
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import { ClientOptions } from '@nimble-way/nimble-js';
|
|
11
|
+
import Nimble from '@nimble-way/nimble-js';
|
|
12
|
+
import { codeTool } from './code-tool';
|
|
13
|
+
import docsSearchTool from './docs-search-tool';
|
|
14
|
+
import { getInstructions } from './instructions';
|
|
15
|
+
import { McpOptions } from './options';
|
|
16
|
+
import { blockedMethodsForCodeTool } from './methods';
|
|
17
|
+
import { HandlerFunction, McpRequestContext, ToolCallResult, McpTool } from './types';
|
|
18
|
+
|
|
19
|
+
export const newMcpServer = async (stainlessApiKey: string | undefined) =>
|
|
20
|
+
new McpServer(
|
|
21
|
+
{
|
|
22
|
+
name: 'nimble_way_nimble_js_api',
|
|
23
|
+
version: '0.5.1',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
instructions: await getInstructions(stainlessApiKey),
|
|
27
|
+
capabilities: { tools: {}, logging: {} },
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
33
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
34
|
+
*/
|
|
35
|
+
export async function initMcpServer(params: {
|
|
36
|
+
server: Server | McpServer;
|
|
37
|
+
clientOptions?: ClientOptions;
|
|
38
|
+
mcpOptions?: McpOptions;
|
|
39
|
+
stainlessApiKey?: string | undefined;
|
|
40
|
+
}) {
|
|
41
|
+
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
42
|
+
|
|
43
|
+
const logAtLevel =
|
|
44
|
+
(level: 'debug' | 'info' | 'warning' | 'error') =>
|
|
45
|
+
(message: string, ...rest: unknown[]) => {
|
|
46
|
+
void server.sendLoggingMessage({
|
|
47
|
+
level,
|
|
48
|
+
data: { message, rest },
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const logger = {
|
|
52
|
+
debug: logAtLevel('debug'),
|
|
53
|
+
info: logAtLevel('info'),
|
|
54
|
+
warn: logAtLevel('warning'),
|
|
55
|
+
error: logAtLevel('error'),
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let _client: Nimble | undefined;
|
|
59
|
+
let _clientError: Error | undefined;
|
|
60
|
+
let _logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off' | undefined;
|
|
61
|
+
|
|
62
|
+
const getClient = (): Nimble => {
|
|
63
|
+
if (_clientError) throw _clientError;
|
|
64
|
+
if (!_client) {
|
|
65
|
+
try {
|
|
66
|
+
_client = new Nimble({
|
|
67
|
+
logger,
|
|
68
|
+
...params.clientOptions,
|
|
69
|
+
defaultHeaders: {
|
|
70
|
+
...params.clientOptions?.defaultHeaders,
|
|
71
|
+
'X-Stainless-MCP': 'true',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
if (_logLevel) {
|
|
75
|
+
_client = _client.withOptions({ logLevel: _logLevel });
|
|
76
|
+
}
|
|
77
|
+
} catch (e) {
|
|
78
|
+
_clientError = e instanceof Error ? e : new Error(String(e));
|
|
79
|
+
throw _clientError;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return _client;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const providedTools = selectTools(params.mcpOptions);
|
|
86
|
+
const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
|
|
87
|
+
|
|
88
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
89
|
+
return {
|
|
90
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
95
|
+
const { name, arguments: args } = request.params;
|
|
96
|
+
const mcpTool = toolMap[name];
|
|
97
|
+
if (!mcpTool) {
|
|
98
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let client: Nimble;
|
|
102
|
+
try {
|
|
103
|
+
client = getClient();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return {
|
|
106
|
+
content: [
|
|
107
|
+
{
|
|
108
|
+
type: 'text' as const,
|
|
109
|
+
text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
isError: true,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return executeHandler({
|
|
117
|
+
handler: mcpTool.handler,
|
|
118
|
+
reqContext: {
|
|
119
|
+
client,
|
|
120
|
+
stainlessApiKey: params.stainlessApiKey ?? params.mcpOptions?.stainlessApiKey,
|
|
121
|
+
},
|
|
122
|
+
args,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
127
|
+
const { level } = request.params;
|
|
128
|
+
let logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off';
|
|
129
|
+
switch (level) {
|
|
130
|
+
case 'debug':
|
|
131
|
+
logLevel = 'debug';
|
|
132
|
+
break;
|
|
133
|
+
case 'info':
|
|
134
|
+
logLevel = 'info';
|
|
135
|
+
break;
|
|
136
|
+
case 'notice':
|
|
137
|
+
case 'warning':
|
|
138
|
+
logLevel = 'warn';
|
|
139
|
+
break;
|
|
140
|
+
case 'error':
|
|
141
|
+
logLevel = 'error';
|
|
142
|
+
break;
|
|
143
|
+
default:
|
|
144
|
+
logLevel = 'off';
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
_logLevel = logLevel;
|
|
148
|
+
if (_client) {
|
|
149
|
+
_client = _client.withOptions({ logLevel });
|
|
150
|
+
}
|
|
151
|
+
return {};
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
157
|
+
*/
|
|
158
|
+
export function selectTools(options?: McpOptions): McpTool[] {
|
|
159
|
+
const includedTools = [
|
|
160
|
+
codeTool({
|
|
161
|
+
blockedMethods: blockedMethodsForCodeTool(options),
|
|
162
|
+
}),
|
|
163
|
+
];
|
|
164
|
+
if (options?.includeDocsTools ?? true) {
|
|
165
|
+
includedTools.push(docsSearchTool);
|
|
166
|
+
}
|
|
167
|
+
return includedTools;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Runs the provided handler with the given client and arguments.
|
|
172
|
+
*/
|
|
173
|
+
export async function executeHandler({
|
|
174
|
+
handler,
|
|
175
|
+
reqContext,
|
|
176
|
+
args,
|
|
177
|
+
}: {
|
|
178
|
+
handler: HandlerFunction;
|
|
179
|
+
reqContext: McpRequestContext;
|
|
180
|
+
args: Record<string, unknown> | undefined;
|
|
181
|
+
}): Promise<ToolCallResult> {
|
|
182
|
+
return await handler({ reqContext, args: args || {} });
|
|
183
|
+
}
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { McpOptions } from './options';
|
|
3
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
4
|
+
|
|
5
|
+
export const launchStdioServer = async (mcpOptions: McpOptions) => {
|
|
6
|
+
const server = await newMcpServer(mcpOptions.stainlessApiKey);
|
|
7
|
+
|
|
8
|
+
await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
|
|
9
|
+
|
|
10
|
+
const transport = new StdioServerTransport();
|
|
11
|
+
await server.connect(transport);
|
|
12
|
+
console.error('MCP Server running on stdio');
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
// this config is included in the published src directory to prevent TS errors
|
|
3
|
+
// from appearing when users go to source, and VSCode opens the source .ts file
|
|
4
|
+
// via declaration maps
|
|
5
|
+
"include": ["index.ts"],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"target": "es2015",
|
|
8
|
+
"lib": ["DOM"],
|
|
9
|
+
"moduleResolution": "node"
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import Nimble from '@nimble-way/nimble-js';
|
|
4
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
|
|
6
|
+
type TextContentBlock = {
|
|
7
|
+
type: 'text';
|
|
8
|
+
text: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type ImageContentBlock = {
|
|
12
|
+
type: 'image';
|
|
13
|
+
data: string;
|
|
14
|
+
mimeType: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type AudioContentBlock = {
|
|
18
|
+
type: 'audio';
|
|
19
|
+
data: string;
|
|
20
|
+
mimeType: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type ResourceContentBlock = {
|
|
24
|
+
type: 'resource';
|
|
25
|
+
resource:
|
|
26
|
+
| {
|
|
27
|
+
uri: string;
|
|
28
|
+
mimeType: string;
|
|
29
|
+
text: string;
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
uri: string;
|
|
33
|
+
mimeType: string;
|
|
34
|
+
blob: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type ContentBlock = TextContentBlock | ImageContentBlock | AudioContentBlock | ResourceContentBlock;
|
|
39
|
+
|
|
40
|
+
export type ToolCallResult = {
|
|
41
|
+
content: ContentBlock[];
|
|
42
|
+
isError?: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type McpRequestContext = {
|
|
46
|
+
client: Nimble;
|
|
47
|
+
stainlessApiKey?: string | undefined;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type HandlerFunction = ({
|
|
51
|
+
reqContext,
|
|
52
|
+
args,
|
|
53
|
+
}: {
|
|
54
|
+
reqContext: McpRequestContext;
|
|
55
|
+
args: Record<string, unknown> | undefined;
|
|
56
|
+
}) => Promise<ToolCallResult>;
|
|
57
|
+
|
|
58
|
+
export function asTextContentResult(result: unknown): ToolCallResult {
|
|
59
|
+
return {
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: 'text',
|
|
63
|
+
text: JSON.stringify(result, null, 2),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function asBinaryContentResult(response: Response): Promise<ToolCallResult> {
|
|
70
|
+
const blob = await response.blob();
|
|
71
|
+
const mimeType = blob.type;
|
|
72
|
+
const data = Buffer.from(await blob.arrayBuffer()).toString('base64');
|
|
73
|
+
if (mimeType.startsWith('image/')) {
|
|
74
|
+
return {
|
|
75
|
+
content: [{ type: 'image', mimeType, data }],
|
|
76
|
+
};
|
|
77
|
+
} else if (mimeType.startsWith('audio/')) {
|
|
78
|
+
return {
|
|
79
|
+
content: [{ type: 'audio', mimeType, data }],
|
|
80
|
+
};
|
|
81
|
+
} else {
|
|
82
|
+
return {
|
|
83
|
+
content: [
|
|
84
|
+
{
|
|
85
|
+
type: 'resource',
|
|
86
|
+
resource: {
|
|
87
|
+
// We must give a URI, even though this isn't actually an MCP resource.
|
|
88
|
+
uri: 'resource://tool-response',
|
|
89
|
+
mimeType,
|
|
90
|
+
blob: data,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function asErrorResult(message: string): ToolCallResult {
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{
|
|
102
|
+
type: 'text',
|
|
103
|
+
text: message,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
isError: true,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type Metadata = {
|
|
111
|
+
resource: string;
|
|
112
|
+
operation: 'read' | 'write';
|
|
113
|
+
tags: string[];
|
|
114
|
+
httpMethod?: string;
|
|
115
|
+
httpPath?: string;
|
|
116
|
+
operationId?: string;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type McpTool = {
|
|
120
|
+
metadata: Metadata;
|
|
121
|
+
tool: Tool;
|
|
122
|
+
handler: HandlerFunction;
|
|
123
|
+
};
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
export const readEnv = (env: string): string | undefined => {
|
|
4
|
+
if (typeof (globalThis as any).process !== 'undefined') {
|
|
5
|
+
return (globalThis as any).process.env?.[env]?.trim();
|
|
6
|
+
} else if (typeof (globalThis as any).Deno !== 'undefined') {
|
|
7
|
+
return (globalThis as any).Deno.env?.get?.(env)?.trim();
|
|
8
|
+
}
|
|
9
|
+
return;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const readEnvOrError = (env: string): string => {
|
|
13
|
+
let envValue = readEnv(env);
|
|
14
|
+
if (envValue === undefined) {
|
|
15
|
+
throw new Error(`Environment variable ${env} is not set`);
|
|
16
|
+
}
|
|
17
|
+
return envValue;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const requireValue = <T>(value: T | undefined, description: string): T => {
|
|
21
|
+
if (value === undefined) {
|
|
22
|
+
throw new Error(`Missing required value: ${description}`);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
};
|
package/stdio.d.mts
ADDED
package/stdio.d.mts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.d.mts","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"OACO,EAAE,UAAU,EAAE;AAGrB,eAAO,MAAM,iBAAiB,GAAU,YAAY,UAAU,kBAQ7D,CAAC"}
|
package/stdio.d.ts
ADDED
package/stdio.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"OACO,EAAE,UAAU,EAAE;AAGrB,eAAO,MAAM,iBAAiB,GAAU,YAAY,UAAU,kBAQ7D,CAAC"}
|
package/stdio.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.launchStdioServer = void 0;
|
|
4
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
5
|
+
const server_1 = require("./server.js");
|
|
6
|
+
const launchStdioServer = async (mcpOptions) => {
|
|
7
|
+
const server = await (0, server_1.newMcpServer)(mcpOptions.stainlessApiKey);
|
|
8
|
+
await (0, server_1.initMcpServer)({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
|
|
9
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
10
|
+
await server.connect(transport);
|
|
11
|
+
console.error('MCP Server running on stdio');
|
|
12
|
+
};
|
|
13
|
+
exports.launchStdioServer = launchStdioServer;
|
|
14
|
+
//# sourceMappingURL=stdio.js.map
|
package/stdio.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":";;;AAAA,wEAAiF;AAEjF,wCAAuD;AAEhD,MAAM,iBAAiB,GAAG,KAAK,EAAE,UAAsB,EAAE,EAAE;IAChE,MAAM,MAAM,GAAG,MAAM,IAAA,qBAAY,EAAC,UAAU,CAAC,eAAe,CAAC,CAAC;IAE9D,MAAM,IAAA,sBAAa,EAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,CAAC,eAAe,EAAE,CAAC,CAAC;IAEzF,MAAM,SAAS,GAAG,IAAI,+BAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;AAC/C,CAAC,CAAC;AARW,QAAA,iBAAiB,qBAQ5B"}
|