context.dev-mcp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +101 -0
  3. package/auth.d.mts +6 -0
  4. package/auth.d.mts.map +1 -0
  5. package/auth.d.ts +6 -0
  6. package/auth.d.ts.map +1 -0
  7. package/auth.js +37 -0
  8. package/auth.js.map +1 -0
  9. package/auth.mjs +32 -0
  10. package/auth.mjs.map +1 -0
  11. package/code-tool-paths.cjs +8 -0
  12. package/code-tool-paths.cjs.map +1 -0
  13. package/code-tool-paths.d.cts +2 -0
  14. package/code-tool-paths.d.cts.map +1 -0
  15. package/code-tool-types.d.mts +14 -0
  16. package/code-tool-types.d.mts.map +1 -0
  17. package/code-tool-types.d.ts +14 -0
  18. package/code-tool-types.d.ts.map +1 -0
  19. package/code-tool-types.js +4 -0
  20. package/code-tool-types.js.map +1 -0
  21. package/code-tool-types.mjs +3 -0
  22. package/code-tool-types.mjs.map +1 -0
  23. package/code-tool-worker.d.mts +5 -0
  24. package/code-tool-worker.d.mts.map +1 -0
  25. package/code-tool-worker.d.ts +5 -0
  26. package/code-tool-worker.d.ts.map +1 -0
  27. package/code-tool-worker.js +244 -0
  28. package/code-tool-worker.js.map +1 -0
  29. package/code-tool-worker.mjs +239 -0
  30. package/code-tool-worker.mjs.map +1 -0
  31. package/code-tool.d.mts +21 -0
  32. package/code-tool.d.mts.map +1 -0
  33. package/code-tool.d.ts +21 -0
  34. package/code-tool.d.ts.map +1 -0
  35. package/code-tool.js +337 -0
  36. package/code-tool.js.map +1 -0
  37. package/code-tool.mjs +301 -0
  38. package/code-tool.mjs.map +1 -0
  39. package/docs-search-tool.d.mts +57 -0
  40. package/docs-search-tool.d.mts.map +1 -0
  41. package/docs-search-tool.d.ts +57 -0
  42. package/docs-search-tool.d.ts.map +1 -0
  43. package/docs-search-tool.js +74 -0
  44. package/docs-search-tool.js.map +1 -0
  45. package/docs-search-tool.mjs +70 -0
  46. package/docs-search-tool.mjs.map +1 -0
  47. package/http.d.mts +12 -0
  48. package/http.d.mts.map +1 -0
  49. package/http.d.ts +12 -0
  50. package/http.d.ts.map +1 -0
  51. package/http.js +142 -0
  52. package/http.js.map +1 -0
  53. package/http.mjs +134 -0
  54. package/http.mjs.map +1 -0
  55. package/index.d.mts +3 -0
  56. package/index.d.mts.map +1 -0
  57. package/index.d.ts +3 -0
  58. package/index.d.ts.map +1 -0
  59. package/index.js +60 -0
  60. package/index.js.map +1 -0
  61. package/index.mjs +58 -0
  62. package/index.mjs.map +1 -0
  63. package/instructions.d.mts +2 -0
  64. package/instructions.d.mts.map +1 -0
  65. package/instructions.d.ts +2 -0
  66. package/instructions.d.ts.map +1 -0
  67. package/instructions.js +42 -0
  68. package/instructions.js.map +1 -0
  69. package/instructions.mjs +39 -0
  70. package/instructions.mjs.map +1 -0
  71. package/logger.d.mts +7 -0
  72. package/logger.d.mts.map +1 -0
  73. package/logger.d.ts +7 -0
  74. package/logger.d.ts.map +1 -0
  75. package/logger.js +29 -0
  76. package/logger.js.map +1 -0
  77. package/logger.mjs +22 -0
  78. package/logger.mjs.map +1 -0
  79. package/methods.d.mts +10 -0
  80. package/methods.d.mts.map +1 -0
  81. package/methods.d.ts +10 -0
  82. package/methods.d.ts.map +1 -0
  83. package/methods.js +185 -0
  84. package/methods.js.map +1 -0
  85. package/methods.mjs +181 -0
  86. package/methods.mjs.map +1 -0
  87. package/options.d.mts +20 -0
  88. package/options.d.mts.map +1 -0
  89. package/options.d.ts +20 -0
  90. package/options.d.ts.map +1 -0
  91. package/options.js +122 -0
  92. package/options.js.map +1 -0
  93. package/options.mjs +115 -0
  94. package/options.mjs.map +1 -0
  95. package/package.json +225 -0
  96. package/server.d.mts +30 -0
  97. package/server.d.mts.map +1 -0
  98. package/server.d.ts +30 -0
  99. package/server.d.ts.map +1 -0
  100. package/server.js +159 -0
  101. package/server.js.map +1 -0
  102. package/server.mjs +149 -0
  103. package/server.mjs.map +1 -0
  104. package/src/auth.ts +42 -0
  105. package/src/code-tool-paths.cts +5 -0
  106. package/src/code-tool-types.ts +17 -0
  107. package/src/code-tool-worker.ts +290 -0
  108. package/src/code-tool.ts +396 -0
  109. package/src/docs-search-tool.ts +100 -0
  110. package/src/http.ts +173 -0
  111. package/src/index.ts +67 -0
  112. package/src/instructions.ts +60 -0
  113. package/src/logger.ts +28 -0
  114. package/src/methods.ts +206 -0
  115. package/src/options.ts +161 -0
  116. package/src/server.ts +190 -0
  117. package/src/stdio.ts +14 -0
  118. package/src/tsconfig.json +11 -0
  119. package/src/types.ts +124 -0
  120. package/src/util.ts +25 -0
  121. package/stdio.d.mts +3 -0
  122. package/stdio.d.mts.map +1 -0
  123. package/stdio.d.ts +3 -0
  124. package/stdio.d.ts.map +1 -0
  125. package/stdio.js +15 -0
  126. package/stdio.js.map +1 -0
  127. package/stdio.mjs +11 -0
  128. package/stdio.mjs.map +1 -0
  129. package/types.d.mts +60 -0
  130. package/types.d.mts.map +1 -0
  131. package/types.d.ts +60 -0
  132. package/types.d.ts.map +1 -0
  133. package/types.js +58 -0
  134. package/types.js.map +1 -0
  135. package/types.mjs +53 -0
  136. package/types.mjs.map +1 -0
  137. package/util.d.mts +4 -0
  138. package/util.d.mts.map +1 -0
  139. package/util.d.ts +4 -0
  140. package/util.d.ts.map +1 -0
  141. package/util.js +30 -0
  142. package/util.js.map +1 -0
  143. package/util.mjs +24 -0
  144. package/util.mjs.map +1 -0
package/src/options.ts ADDED
@@ -0,0 +1,161 @@
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
+ logFormat: 'json' | 'pretty';
12
+ transport: 'stdio' | 'http';
13
+ port: number | undefined;
14
+ socket: string | undefined;
15
+ };
16
+
17
+ export type McpOptions = {
18
+ includeCodeTool?: boolean | undefined;
19
+ includeDocsTools?: boolean | undefined;
20
+ stainlessApiKey?: string | undefined;
21
+ codeAllowHttpGets?: boolean | undefined;
22
+ codeAllowedMethods?: string[] | undefined;
23
+ codeBlockedMethods?: string[] | undefined;
24
+ codeExecutionMode: McpCodeExecutionMode;
25
+ };
26
+
27
+ export type McpCodeExecutionMode = 'stainless-sandbox' | 'local';
28
+
29
+ export function parseCLIOptions(): CLIOptions {
30
+ const opts = yargs(hideBin(process.argv))
31
+ .option('code-allow-http-gets', {
32
+ type: 'boolean',
33
+ description:
34
+ 'Allow all code tool methods that map to HTTP GET operations. If all code-allow-* flags are unset, then everything is allowed.',
35
+ })
36
+ .option('code-allowed-methods', {
37
+ type: 'string',
38
+ array: true,
39
+ description:
40
+ '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.',
41
+ })
42
+ .option('code-blocked-methods', {
43
+ type: 'string',
44
+ array: true,
45
+ description:
46
+ '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.',
47
+ })
48
+ .option('code-execution-mode', {
49
+ type: 'string',
50
+ choices: ['stainless-sandbox', 'local'],
51
+ default: 'stainless-sandbox',
52
+ description:
53
+ "Where to run code execution in code tool; 'stainless-sandbox' will execute code in Stainless-hosted sandboxes whereas 'local' will execute code locally on the MCP server machine.",
54
+ })
55
+ .option('debug', { type: 'boolean', description: 'Enable debug logging' })
56
+ .option('log-format', {
57
+ type: 'string',
58
+ choices: ['json', 'pretty'],
59
+ description: 'Format for log output; defaults to json unless tty is detected',
60
+ })
61
+ .option('no-tools', {
62
+ type: 'string',
63
+ array: true,
64
+ choices: ['code', 'docs'],
65
+ description: 'Tools to explicitly disable',
66
+ })
67
+ .option('port', {
68
+ type: 'number',
69
+ default: 3000,
70
+ description: 'Port to serve on if using http transport',
71
+ })
72
+ .option('socket', { type: 'string', description: 'Unix socket to serve on if using http transport' })
73
+ .option('stainless-api-key', {
74
+ type: 'string',
75
+ default: readEnv('STAINLESS_API_KEY'),
76
+ description:
77
+ 'API key for Stainless. Used to authenticate requests to Stainless-hosted tools endpoints.',
78
+ })
79
+ .option('tools', {
80
+ type: 'string',
81
+ array: true,
82
+ choices: ['code', 'docs'],
83
+ description: 'Tools to explicitly enable',
84
+ })
85
+ .option('transport', {
86
+ type: 'string',
87
+ choices: ['stdio', 'http'],
88
+ default: 'stdio',
89
+ description: 'What transport to use; stdio for local servers or http for remote servers',
90
+ })
91
+ .env('MCP_SERVER')
92
+ .version(true)
93
+ .help();
94
+
95
+ const argv = opts.parseSync();
96
+
97
+ const shouldIncludeToolType = (toolType: 'code' | 'docs') =>
98
+ argv.noTools?.includes(toolType) ? false
99
+ : argv.tools?.includes(toolType) ? true
100
+ : undefined;
101
+
102
+ const includeCodeTool = shouldIncludeToolType('code');
103
+ const includeDocsTools = shouldIncludeToolType('docs');
104
+
105
+ const transport = argv.transport as 'stdio' | 'http';
106
+ const logFormat =
107
+ argv.logFormat ? (argv.logFormat as 'json' | 'pretty')
108
+ : process.stderr.isTTY ? 'pretty'
109
+ : 'json';
110
+
111
+ return {
112
+ ...(includeCodeTool !== undefined && { includeCodeTool }),
113
+ ...(includeDocsTools !== undefined && { includeDocsTools }),
114
+ debug: !!argv.debug,
115
+ stainlessApiKey: argv.stainlessApiKey,
116
+ codeAllowHttpGets: argv.codeAllowHttpGets,
117
+ codeAllowedMethods: argv.codeAllowedMethods,
118
+ codeBlockedMethods: argv.codeBlockedMethods,
119
+ codeExecutionMode: argv.codeExecutionMode as McpCodeExecutionMode,
120
+ transport,
121
+ logFormat,
122
+ port: argv.port,
123
+ socket: argv.socket,
124
+ };
125
+ }
126
+
127
+ const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
128
+ z.preprocess(
129
+ (val) =>
130
+ Array.isArray(val) ? val
131
+ : val ? [val]
132
+ : val,
133
+ z.array(zodType).optional(),
134
+ );
135
+
136
+ const QueryOptions = z.object({
137
+ tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to use'),
138
+ no_tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to not use.'),
139
+ tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
140
+ });
141
+
142
+ export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): McpOptions {
143
+ const queryObject = typeof query === 'string' ? qs.parse(query) : query;
144
+ const queryOptions = QueryOptions.parse(queryObject);
145
+
146
+ let codeTool: boolean | undefined =
147
+ queryOptions.no_tools && queryOptions.no_tools?.includes('code') ? false
148
+ : queryOptions.tools?.includes('code') ? true
149
+ : defaultOptions.includeCodeTool;
150
+
151
+ let docsTools: boolean | undefined =
152
+ queryOptions.no_tools && queryOptions.no_tools?.includes('docs') ? false
153
+ : queryOptions.tools?.includes('docs') ? true
154
+ : defaultOptions.includeDocsTools;
155
+
156
+ return {
157
+ ...(codeTool !== undefined && { includeCodeTool: codeTool }),
158
+ ...(docsTools !== undefined && { includeDocsTools: docsTools }),
159
+ codeExecutionMode: defaultOptions.codeExecutionMode,
160
+ };
161
+ }
package/src/server.ts ADDED
@@ -0,0 +1,190 @@
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 'context.dev';
11
+ import ContextDev from 'context.dev';
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: 'context_dev_api',
23
+ version: '0.1.0',
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
+ upstreamClientEnvs?: Record<string, string> | undefined;
41
+ }) {
42
+ const server = params.server instanceof McpServer ? params.server.server : params.server;
43
+
44
+ const logAtLevel =
45
+ (level: 'debug' | 'info' | 'warning' | 'error') =>
46
+ (message: string, ...rest: unknown[]) => {
47
+ void server.sendLoggingMessage({
48
+ level,
49
+ data: { message, rest },
50
+ });
51
+ };
52
+ const logger = {
53
+ debug: logAtLevel('debug'),
54
+ info: logAtLevel('info'),
55
+ warn: logAtLevel('warning'),
56
+ error: logAtLevel('error'),
57
+ };
58
+
59
+ let _client: ContextDev | undefined;
60
+ let _clientError: Error | undefined;
61
+ let _logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off' | undefined;
62
+
63
+ const getClient = (): ContextDev => {
64
+ if (_clientError) throw _clientError;
65
+ if (!_client) {
66
+ try {
67
+ _client = new ContextDev({
68
+ logger,
69
+ ...params.clientOptions,
70
+ defaultHeaders: {
71
+ ...params.clientOptions?.defaultHeaders,
72
+ 'X-Stainless-MCP': 'true',
73
+ },
74
+ });
75
+ if (_logLevel) {
76
+ _client = _client.withOptions({ logLevel: _logLevel });
77
+ }
78
+ } catch (e) {
79
+ _clientError = e instanceof Error ? e : new Error(String(e));
80
+ throw _clientError;
81
+ }
82
+ }
83
+ return _client;
84
+ };
85
+
86
+ const providedTools = selectTools(params.mcpOptions);
87
+ const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
88
+
89
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
90
+ return {
91
+ tools: providedTools.map((mcpTool) => mcpTool.tool),
92
+ };
93
+ });
94
+
95
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
96
+ const { name, arguments: args } = request.params;
97
+ const mcpTool = toolMap[name];
98
+ if (!mcpTool) {
99
+ throw new Error(`Unknown tool: ${name}`);
100
+ }
101
+
102
+ let client: ContextDev;
103
+ try {
104
+ client = getClient();
105
+ } catch (error) {
106
+ return {
107
+ content: [
108
+ {
109
+ type: 'text' as const,
110
+ text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
111
+ },
112
+ ],
113
+ isError: true,
114
+ };
115
+ }
116
+
117
+ return executeHandler({
118
+ handler: mcpTool.handler,
119
+ reqContext: {
120
+ client,
121
+ stainlessApiKey: params.stainlessApiKey ?? params.mcpOptions?.stainlessApiKey,
122
+ upstreamClientEnvs: params.upstreamClientEnvs,
123
+ },
124
+ args,
125
+ });
126
+ });
127
+
128
+ server.setRequestHandler(SetLevelRequestSchema, async (request) => {
129
+ const { level } = request.params;
130
+ let logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off';
131
+ switch (level) {
132
+ case 'debug':
133
+ logLevel = 'debug';
134
+ break;
135
+ case 'info':
136
+ logLevel = 'info';
137
+ break;
138
+ case 'notice':
139
+ case 'warning':
140
+ logLevel = 'warn';
141
+ break;
142
+ case 'error':
143
+ logLevel = 'error';
144
+ break;
145
+ default:
146
+ logLevel = 'off';
147
+ break;
148
+ }
149
+ _logLevel = logLevel;
150
+ if (_client) {
151
+ _client = _client.withOptions({ logLevel });
152
+ }
153
+ return {};
154
+ });
155
+ }
156
+
157
+ /**
158
+ * Selects the tools to include in the MCP Server based on the provided options.
159
+ */
160
+ export function selectTools(options?: McpOptions): McpTool[] {
161
+ const includedTools = [];
162
+
163
+ if (options?.includeCodeTool ?? true) {
164
+ includedTools.push(
165
+ codeTool({
166
+ blockedMethods: blockedMethodsForCodeTool(options),
167
+ codeExecutionMode: options?.codeExecutionMode ?? 'stainless-sandbox',
168
+ }),
169
+ );
170
+ }
171
+ if (options?.includeDocsTools ?? true) {
172
+ includedTools.push(docsSearchTool);
173
+ }
174
+ return includedTools;
175
+ }
176
+
177
+ /**
178
+ * Runs the provided handler with the given client and arguments.
179
+ */
180
+ export async function executeHandler({
181
+ handler,
182
+ reqContext,
183
+ args,
184
+ }: {
185
+ handler: HandlerFunction;
186
+ reqContext: McpRequestContext;
187
+ args: Record<string, unknown> | undefined;
188
+ }): Promise<ToolCallResult> {
189
+ return await handler({ reqContext, args: args || {} });
190
+ }
package/src/stdio.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
2
+ import { McpOptions } from './options';
3
+ import { initMcpServer, newMcpServer } from './server';
4
+ import { getLogger } from './logger';
5
+
6
+ export const launchStdioServer = async (mcpOptions: McpOptions) => {
7
+ const server = await newMcpServer(mcpOptions.stainlessApiKey);
8
+
9
+ await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
10
+
11
+ const transport = new StdioServerTransport();
12
+ await server.connect(transport);
13
+ getLogger().info('MCP Server running on stdio');
14
+ };
@@ -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,124 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import ContextDev from 'context.dev';
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: ContextDev;
47
+ stainlessApiKey?: string | undefined;
48
+ upstreamClientEnvs?: Record<string, string> | undefined;
49
+ };
50
+
51
+ export type HandlerFunction = ({
52
+ reqContext,
53
+ args,
54
+ }: {
55
+ reqContext: McpRequestContext;
56
+ args: Record<string, unknown> | undefined;
57
+ }) => Promise<ToolCallResult>;
58
+
59
+ export function asTextContentResult(result: unknown): ToolCallResult {
60
+ return {
61
+ content: [
62
+ {
63
+ type: 'text',
64
+ text: JSON.stringify(result, null, 2),
65
+ },
66
+ ],
67
+ };
68
+ }
69
+
70
+ export async function asBinaryContentResult(response: Response): Promise<ToolCallResult> {
71
+ const blob = await response.blob();
72
+ const mimeType = blob.type;
73
+ const data = Buffer.from(await blob.arrayBuffer()).toString('base64');
74
+ if (mimeType.startsWith('image/')) {
75
+ return {
76
+ content: [{ type: 'image', mimeType, data }],
77
+ };
78
+ } else if (mimeType.startsWith('audio/')) {
79
+ return {
80
+ content: [{ type: 'audio', mimeType, data }],
81
+ };
82
+ } else {
83
+ return {
84
+ content: [
85
+ {
86
+ type: 'resource',
87
+ resource: {
88
+ // We must give a URI, even though this isn't actually an MCP resource.
89
+ uri: 'resource://tool-response',
90
+ mimeType,
91
+ blob: data,
92
+ },
93
+ },
94
+ ],
95
+ };
96
+ }
97
+ }
98
+
99
+ export function asErrorResult(message: string): ToolCallResult {
100
+ return {
101
+ content: [
102
+ {
103
+ type: 'text',
104
+ text: message,
105
+ },
106
+ ],
107
+ isError: true,
108
+ };
109
+ }
110
+
111
+ export type Metadata = {
112
+ resource: string;
113
+ operation: 'read' | 'write';
114
+ tags: string[];
115
+ httpMethod?: string;
116
+ httpPath?: string;
117
+ operationId?: string;
118
+ };
119
+
120
+ export type McpTool = {
121
+ metadata: Metadata;
122
+ tool: Tool;
123
+ handler: HandlerFunction;
124
+ };
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
@@ -0,0 +1,3 @@
1
+ import { McpOptions } from "./options.mjs";
2
+ export declare const launchStdioServer: (mcpOptions: McpOptions) => Promise<void>;
3
+ //# sourceMappingURL=stdio.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.d.mts","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"OACO,EAAE,UAAU,EAAE;AAIrB,eAAO,MAAM,iBAAiB,GAAU,YAAY,UAAU,kBAQ7D,CAAC"}
package/stdio.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { McpOptions } from "./options.js";
2
+ export declare const launchStdioServer: (mcpOptions: McpOptions) => Promise<void>;
3
+ //# sourceMappingURL=stdio.d.ts.map
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;AAIrB,eAAO,MAAM,iBAAiB,GAAU,YAAY,UAAU,kBAQ7D,CAAC"}
package/stdio.js ADDED
@@ -0,0 +1,15 @@
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 logger_1 = require("./logger.js");
7
+ const launchStdioServer = async (mcpOptions) => {
8
+ const server = await (0, server_1.newMcpServer)(mcpOptions.stainlessApiKey);
9
+ await (0, server_1.initMcpServer)({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
10
+ const transport = new stdio_js_1.StdioServerTransport();
11
+ await server.connect(transport);
12
+ (0, logger_1.getLogger)().info('MCP Server running on stdio');
13
+ };
14
+ exports.launchStdioServer = launchStdioServer;
15
+ //# 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;AACvD,wCAAqC;AAE9B,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,IAAA,kBAAS,GAAE,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;AAClD,CAAC,CAAC;AARW,QAAA,iBAAiB,qBAQ5B"}
package/stdio.mjs ADDED
@@ -0,0 +1,11 @@
1
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
2
+ import { initMcpServer, newMcpServer } from "./server.mjs";
3
+ import { getLogger } from "./logger.mjs";
4
+ export const launchStdioServer = async (mcpOptions) => {
5
+ const server = await newMcpServer(mcpOptions.stainlessApiKey);
6
+ await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
7
+ const transport = new StdioServerTransport();
8
+ await server.connect(transport);
9
+ getLogger().info('MCP Server running on stdio');
10
+ };
11
+ //# sourceMappingURL=stdio.mjs.map
package/stdio.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.mjs","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C;OAEzE,EAAE,aAAa,EAAE,YAAY,EAAE;OAC/B,EAAE,SAAS,EAAE;AAEpB,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,UAAsB,EAAE,EAAE;IAChE,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC;IAE9D,MAAM,aAAa,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,CAAC,eAAe,EAAE,CAAC,CAAC;IAEzF,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,SAAS,EAAE,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;AAClD,CAAC,CAAC"}
package/types.d.mts ADDED
@@ -0,0 +1,60 @@
1
+ import ContextDev from 'context.dev';
2
+ import { Tool } from '@modelcontextprotocol/sdk/types.js';
3
+ type TextContentBlock = {
4
+ type: 'text';
5
+ text: string;
6
+ };
7
+ type ImageContentBlock = {
8
+ type: 'image';
9
+ data: string;
10
+ mimeType: string;
11
+ };
12
+ type AudioContentBlock = {
13
+ type: 'audio';
14
+ data: string;
15
+ mimeType: string;
16
+ };
17
+ type ResourceContentBlock = {
18
+ type: 'resource';
19
+ resource: {
20
+ uri: string;
21
+ mimeType: string;
22
+ text: string;
23
+ } | {
24
+ uri: string;
25
+ mimeType: string;
26
+ blob: string;
27
+ };
28
+ };
29
+ export type ContentBlock = TextContentBlock | ImageContentBlock | AudioContentBlock | ResourceContentBlock;
30
+ export type ToolCallResult = {
31
+ content: ContentBlock[];
32
+ isError?: boolean;
33
+ };
34
+ export type McpRequestContext = {
35
+ client: ContextDev;
36
+ stainlessApiKey?: string | undefined;
37
+ upstreamClientEnvs?: Record<string, string> | undefined;
38
+ };
39
+ export type HandlerFunction = ({ reqContext, args, }: {
40
+ reqContext: McpRequestContext;
41
+ args: Record<string, unknown> | undefined;
42
+ }) => Promise<ToolCallResult>;
43
+ export declare function asTextContentResult(result: unknown): ToolCallResult;
44
+ export declare function asBinaryContentResult(response: Response): Promise<ToolCallResult>;
45
+ export declare function asErrorResult(message: string): ToolCallResult;
46
+ export type Metadata = {
47
+ resource: string;
48
+ operation: 'read' | 'write';
49
+ tags: string[];
50
+ httpMethod?: string;
51
+ httpPath?: string;
52
+ operationId?: string;
53
+ };
54
+ export type McpTool = {
55
+ metadata: Metadata;
56
+ tool: Tool;
57
+ handler: HandlerFunction;
58
+ };
59
+ export {};
60
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["src/types.ts"],"names":[],"mappings":"OAEO,UAAU,MAAM,aAAa;OAC7B,EAAE,IAAI,EAAE,MAAM,oCAAoC;AAEzD,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,oBAAoB,GAAG;IAC1B,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EACJ;QACE,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;KACd,GACD;QACE,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACP,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,gBAAgB,GAAG,iBAAiB,GAAG,iBAAiB,GAAG,oBAAoB,CAAC;AAE3G,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,UAAU,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,kBAAkB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;CACzD,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,CAAC,EAC7B,UAAU,EACV,IAAI,GACL,EAAE;IACD,UAAU,EAAE,iBAAiB,CAAC;IAC9B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CAC3C,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;AAE9B,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,OAAO,GAAG,cAAc,CASnE;AAED,wBAAsB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,cAAc,CAAC,CA2BvF;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,CAU7D;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,eAAe,CAAC;CAC1B,CAAC"}