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.
- 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-paths.cjs +8 -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 +244 -0
- package/code-tool-worker.js.map +1 -0
- package/code-tool-worker.mjs +239 -0
- package/code-tool-worker.mjs.map +1 -0
- package/code-tool.d.mts +21 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +21 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +337 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +301 -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 +74 -0
- package/docs-search-tool.js.map +1 -0
- package/docs-search-tool.mjs +70 -0
- package/docs-search-tool.mjs.map +1 -0
- package/http.d.mts +12 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +12 -0
- package/http.d.ts.map +1 -0
- package/http.js +142 -0
- package/http.js.map +1 -0
- package/http.mjs +134 -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 +60 -0
- package/index.js.map +1 -0
- package/index.mjs +58 -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 +42 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +39 -0
- package/instructions.mjs.map +1 -0
- package/logger.d.mts +7 -0
- package/logger.d.mts.map +1 -0
- package/logger.d.ts +7 -0
- package/logger.d.ts.map +1 -0
- package/logger.js +29 -0
- package/logger.js.map +1 -0
- package/logger.mjs +22 -0
- package/logger.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 +185 -0
- package/methods.js.map +1 -0
- package/methods.mjs +181 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +20 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +20 -0
- package/options.d.ts.map +1 -0
- package/options.js +122 -0
- package/options.js.map +1 -0
- package/options.mjs +115 -0
- package/options.mjs.map +1 -0
- package/package.json +225 -0
- package/server.d.mts +30 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +30 -0
- package/server.d.ts.map +1 -0
- package/server.js +159 -0
- package/server.js.map +1 -0
- package/server.mjs +149 -0
- package/server.mjs.map +1 -0
- package/src/auth.ts +42 -0
- package/src/code-tool-paths.cts +5 -0
- package/src/code-tool-types.ts +17 -0
- package/src/code-tool-worker.ts +290 -0
- package/src/code-tool.ts +396 -0
- package/src/docs-search-tool.ts +100 -0
- package/src/http.ts +173 -0
- package/src/index.ts +67 -0
- package/src/instructions.ts +60 -0
- package/src/logger.ts +28 -0
- package/src/methods.ts +206 -0
- package/src/options.ts +161 -0
- package/src/server.ts +190 -0
- package/src/stdio.ts +14 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +124 -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 +15 -0
- package/stdio.js.map +1 -0
- package/stdio.mjs +11 -0
- package/stdio.mjs.map +1 -0
- package/types.d.mts +60 -0
- package/types.d.mts.map +1 -0
- package/types.d.ts +60 -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
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { Metadata, McpRequestContext, asTextContentResult } from './types';
|
|
5
|
+
import { getLogger } from './logger';
|
|
6
|
+
|
|
7
|
+
export const metadata: Metadata = {
|
|
8
|
+
resource: 'all',
|
|
9
|
+
operation: 'read',
|
|
10
|
+
tags: [],
|
|
11
|
+
httpMethod: 'get',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const tool: Tool = {
|
|
15
|
+
name: 'search_docs',
|
|
16
|
+
description:
|
|
17
|
+
'Search SDK documentation to find methods, parameters, and usage examples for interacting with the API. Use this before writing code when you need to discover the right approach.',
|
|
18
|
+
inputSchema: {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
query: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
description: 'The query to search for.',
|
|
24
|
+
},
|
|
25
|
+
language: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: 'The language for the SDK to search for.',
|
|
28
|
+
enum: ['http', 'python', 'go', 'typescript', 'javascript', 'terraform', 'ruby', 'java', 'kotlin'],
|
|
29
|
+
},
|
|
30
|
+
detail: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
description: 'The amount of detail to return.',
|
|
33
|
+
enum: ['default', 'verbose'],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ['query', 'language'],
|
|
37
|
+
},
|
|
38
|
+
annotations: {
|
|
39
|
+
readOnlyHint: true,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const docsSearchURL =
|
|
44
|
+
process.env['DOCS_SEARCH_URL'] || 'https://api.stainless.com/api/projects/context.dev/docs/search';
|
|
45
|
+
|
|
46
|
+
export const handler = async ({
|
|
47
|
+
reqContext,
|
|
48
|
+
args,
|
|
49
|
+
}: {
|
|
50
|
+
reqContext: McpRequestContext;
|
|
51
|
+
args: Record<string, unknown> | undefined;
|
|
52
|
+
}) => {
|
|
53
|
+
const body = args as any;
|
|
54
|
+
const query = new URLSearchParams(body).toString();
|
|
55
|
+
|
|
56
|
+
const startTime = Date.now();
|
|
57
|
+
const result = await fetch(`${docsSearchURL}?${query}`, {
|
|
58
|
+
headers: {
|
|
59
|
+
...(reqContext.stainlessApiKey && { Authorization: reqContext.stainlessApiKey }),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const logger = getLogger();
|
|
64
|
+
|
|
65
|
+
if (!result.ok) {
|
|
66
|
+
const errorText = await result.text();
|
|
67
|
+
logger.warn(
|
|
68
|
+
{
|
|
69
|
+
durationMs: Date.now() - startTime,
|
|
70
|
+
query: body.query,
|
|
71
|
+
status: result.status,
|
|
72
|
+
statusText: result.statusText,
|
|
73
|
+
errorText,
|
|
74
|
+
},
|
|
75
|
+
'Got error response from docs search tool',
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
if (result.status === 404 && !reqContext.stainlessApiKey) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'Could not find docs for this project. You may need to provide a Stainless API key via the STAINLESS_API_KEY environment variable, the --stainless-api-key flag, or the x-stainless-api-key HTTP header.',
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
throw new Error(
|
|
85
|
+
`${result.status}: ${result.statusText} when using doc search tool. Details: ${errorText}`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const resultBody = await result.json();
|
|
90
|
+
logger.info(
|
|
91
|
+
{
|
|
92
|
+
durationMs: Date.now() - startTime,
|
|
93
|
+
query: body.query,
|
|
94
|
+
},
|
|
95
|
+
'Got docs search result',
|
|
96
|
+
);
|
|
97
|
+
return asTextContentResult(resultBody);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export default { metadata, tool, handler };
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
import { ClientOptions } from 'context.dev';
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import pino from 'pino';
|
|
8
|
+
import pinoHttp from 'pino-http';
|
|
9
|
+
import { getStainlessApiKey, parseClientAuthHeaders } from './auth';
|
|
10
|
+
import { getLogger } from './logger';
|
|
11
|
+
import { McpOptions } from './options';
|
|
12
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
13
|
+
|
|
14
|
+
const newServer = async ({
|
|
15
|
+
clientOptions,
|
|
16
|
+
mcpOptions,
|
|
17
|
+
req,
|
|
18
|
+
res,
|
|
19
|
+
}: {
|
|
20
|
+
clientOptions: ClientOptions;
|
|
21
|
+
mcpOptions: McpOptions;
|
|
22
|
+
req: express.Request;
|
|
23
|
+
res: express.Response;
|
|
24
|
+
}): Promise<McpServer | null> => {
|
|
25
|
+
const stainlessApiKey = getStainlessApiKey(req, mcpOptions);
|
|
26
|
+
const server = await newMcpServer(stainlessApiKey);
|
|
27
|
+
|
|
28
|
+
const authOptions = parseClientAuthHeaders(req, false);
|
|
29
|
+
|
|
30
|
+
let upstreamClientEnvs: Record<string, string> | undefined;
|
|
31
|
+
const clientEnvsHeader = req.headers['x-stainless-mcp-client-envs'];
|
|
32
|
+
if (typeof clientEnvsHeader === 'string') {
|
|
33
|
+
try {
|
|
34
|
+
const parsed = JSON.parse(clientEnvsHeader);
|
|
35
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
36
|
+
upstreamClientEnvs = parsed;
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
// Ignore malformed header
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await initMcpServer({
|
|
44
|
+
server: server,
|
|
45
|
+
mcpOptions: mcpOptions,
|
|
46
|
+
clientOptions: {
|
|
47
|
+
...clientOptions,
|
|
48
|
+
...authOptions,
|
|
49
|
+
},
|
|
50
|
+
stainlessApiKey: stainlessApiKey,
|
|
51
|
+
upstreamClientEnvs,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return server;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const post =
|
|
58
|
+
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
|
|
59
|
+
async (req: express.Request, res: express.Response) => {
|
|
60
|
+
const server = await newServer({ ...options, req, res });
|
|
61
|
+
// If we return null, we already set the authorization error.
|
|
62
|
+
if (server === null) return;
|
|
63
|
+
const transport = new StreamableHTTPServerTransport();
|
|
64
|
+
await server.connect(transport as any);
|
|
65
|
+
await transport.handleRequest(req, res, req.body);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const get = async (req: express.Request, res: express.Response) => {
|
|
69
|
+
res.status(405).json({
|
|
70
|
+
jsonrpc: '2.0',
|
|
71
|
+
error: {
|
|
72
|
+
code: -32000,
|
|
73
|
+
message: 'Method not supported',
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const del = async (req: express.Request, res: express.Response) => {
|
|
79
|
+
res.status(405).json({
|
|
80
|
+
jsonrpc: '2.0',
|
|
81
|
+
error: {
|
|
82
|
+
code: -32000,
|
|
83
|
+
message: 'Method not supported',
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const redactHeaders = (headers: Record<string, any>) => {
|
|
89
|
+
const hiddenHeaders = /auth|cookie|key|token|x-stainless-mcp-client-envs/i;
|
|
90
|
+
const filtered = { ...headers };
|
|
91
|
+
Object.keys(filtered).forEach((key) => {
|
|
92
|
+
if (hiddenHeaders.test(key)) {
|
|
93
|
+
filtered[key] = '[REDACTED]';
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return filtered;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const streamableHTTPApp = ({
|
|
100
|
+
clientOptions = {},
|
|
101
|
+
mcpOptions,
|
|
102
|
+
}: {
|
|
103
|
+
clientOptions?: ClientOptions;
|
|
104
|
+
mcpOptions: McpOptions;
|
|
105
|
+
}): express.Express => {
|
|
106
|
+
const app = express();
|
|
107
|
+
app.set('query parser', 'extended');
|
|
108
|
+
app.use(express.json());
|
|
109
|
+
app.use(
|
|
110
|
+
pinoHttp({
|
|
111
|
+
logger: getLogger(),
|
|
112
|
+
customLogLevel: (req, res) => {
|
|
113
|
+
if (res.statusCode >= 500) {
|
|
114
|
+
return 'error';
|
|
115
|
+
} else if (res.statusCode >= 400) {
|
|
116
|
+
return 'warn';
|
|
117
|
+
}
|
|
118
|
+
return 'info';
|
|
119
|
+
},
|
|
120
|
+
customSuccessMessage: function (req, res) {
|
|
121
|
+
return `Request ${req.method} to ${req.url} completed with status ${res.statusCode}`;
|
|
122
|
+
},
|
|
123
|
+
customErrorMessage: function (req, res, err) {
|
|
124
|
+
return `Request ${req.method} to ${req.url} errored with status ${res.statusCode}`;
|
|
125
|
+
},
|
|
126
|
+
serializers: {
|
|
127
|
+
req: pino.stdSerializers.wrapRequestSerializer((req) => {
|
|
128
|
+
return {
|
|
129
|
+
...req,
|
|
130
|
+
headers: redactHeaders(req.raw.headers),
|
|
131
|
+
};
|
|
132
|
+
}),
|
|
133
|
+
res: pino.stdSerializers.wrapResponseSerializer((res) => {
|
|
134
|
+
return {
|
|
135
|
+
...res,
|
|
136
|
+
headers: redactHeaders(res.headers),
|
|
137
|
+
};
|
|
138
|
+
}),
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
app.get('/health', async (req: express.Request, res: express.Response) => {
|
|
144
|
+
res.status(200).send('OK');
|
|
145
|
+
});
|
|
146
|
+
app.get('/', get);
|
|
147
|
+
app.post('/', post({ clientOptions, mcpOptions }));
|
|
148
|
+
app.delete('/', del);
|
|
149
|
+
|
|
150
|
+
return app;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export const launchStreamableHTTPServer = async ({
|
|
154
|
+
mcpOptions,
|
|
155
|
+
port,
|
|
156
|
+
}: {
|
|
157
|
+
mcpOptions: McpOptions;
|
|
158
|
+
port: number | string | undefined;
|
|
159
|
+
}) => {
|
|
160
|
+
const app = streamableHTTPApp({ mcpOptions });
|
|
161
|
+
const server = app.listen(port);
|
|
162
|
+
const address = server.address();
|
|
163
|
+
|
|
164
|
+
const logger = getLogger();
|
|
165
|
+
|
|
166
|
+
if (typeof address === 'string') {
|
|
167
|
+
logger.info(`MCP Server running on streamable HTTP at ${address}`);
|
|
168
|
+
} else if (address !== null) {
|
|
169
|
+
logger.info(`MCP Server running on streamable HTTP on port ${address.port}`);
|
|
170
|
+
} else {
|
|
171
|
+
logger.info(`MCP Server running on streamable HTTP on port ${port}`);
|
|
172
|
+
}
|
|
173
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { selectTools } from './server';
|
|
4
|
+
import { McpOptions, parseCLIOptions } from './options';
|
|
5
|
+
import { launchStdioServer } from './stdio';
|
|
6
|
+
import { launchStreamableHTTPServer } from './http';
|
|
7
|
+
import type { McpTool } from './types';
|
|
8
|
+
import { configureLogger, getLogger } from './logger';
|
|
9
|
+
|
|
10
|
+
async function main() {
|
|
11
|
+
const options = parseOptionsOrError();
|
|
12
|
+
configureLogger({
|
|
13
|
+
level: options.debug ? 'debug' : 'info',
|
|
14
|
+
pretty: options.logFormat === 'pretty',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const selectedTools = await selectToolsOrError(options);
|
|
18
|
+
|
|
19
|
+
getLogger().info(
|
|
20
|
+
{ tools: selectedTools.map((e) => e.tool.name) },
|
|
21
|
+
`MCP Server starting with ${selectedTools.length} tools`,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
switch (options.transport) {
|
|
25
|
+
case 'stdio':
|
|
26
|
+
await launchStdioServer(options);
|
|
27
|
+
break;
|
|
28
|
+
case 'http':
|
|
29
|
+
await launchStreamableHTTPServer({
|
|
30
|
+
mcpOptions: options,
|
|
31
|
+
port: options.socket ?? options.port,
|
|
32
|
+
});
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (require.main === module) {
|
|
38
|
+
main().catch((error) => {
|
|
39
|
+
// Logger might not be initialized yet
|
|
40
|
+
console.error('Fatal error in main()', error);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseOptionsOrError() {
|
|
46
|
+
try {
|
|
47
|
+
return parseCLIOptions();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
// Logger is initialized after options, so use console.error here
|
|
50
|
+
console.error('Error parsing options', error);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
|
|
56
|
+
try {
|
|
57
|
+
const includedTools = selectTools(options);
|
|
58
|
+
if (includedTools.length === 0) {
|
|
59
|
+
getLogger().error('No tools match the provided filters');
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
return includedTools;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
getLogger().error({ error }, 'Error filtering tools');
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { readEnv } from './util';
|
|
4
|
+
import { getLogger } from './logger';
|
|
5
|
+
|
|
6
|
+
const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
|
|
7
|
+
|
|
8
|
+
interface InstructionsCacheEntry {
|
|
9
|
+
fetchedInstructions: string;
|
|
10
|
+
fetchedAt: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const instructionsCache = new Map<string, InstructionsCacheEntry>();
|
|
14
|
+
|
|
15
|
+
export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
const cacheKey = stainlessApiKey ?? '';
|
|
18
|
+
const cached = instructionsCache.get(cacheKey);
|
|
19
|
+
|
|
20
|
+
if (cached && now - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
|
|
21
|
+
return cached.fetchedInstructions;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Evict stale entries so the cache doesn't grow unboundedly.
|
|
25
|
+
for (const [key, entry] of instructionsCache) {
|
|
26
|
+
if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
|
|
27
|
+
instructionsCache.delete(key);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
|
|
32
|
+
instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: now });
|
|
33
|
+
return fetchedInstructions;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
|
|
37
|
+
// Setting the stainless API key is optional, but may be required
|
|
38
|
+
// to authenticate requests to the Stainless API.
|
|
39
|
+
const response = await fetch(
|
|
40
|
+
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/context.dev',
|
|
41
|
+
{
|
|
42
|
+
method: 'GET',
|
|
43
|
+
headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
let instructions: string | undefined;
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
getLogger().warn(
|
|
50
|
+
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
instructions =
|
|
54
|
+
'\n This is the context.dev MCP server.\n\n Available tools:\n - search_docs: Search SDK documentation to find the right methods and parameters.\n - execute: Run TypeScript code against a pre-authenticated SDK client. Define an async run(client) function.\n\n Workflow:\n - If unsure about the API, call search_docs first.\n - Write complete solutions in a single execute call when possible. For large datasets, use API filters to narrow results or paginate within a single execute block.\n - If execute returns an error, read the error and fix your code rather than retrying the same approach.\n - Variables do not persist between execute calls. Return or log all data you need.\n - Individual HTTP requests to the API have a 30-second timeout. If a request times out, try a smaller query or add filters.\n - Code execution has a total timeout of approximately 5 minutes. If your code times out, simplify it or break it into smaller steps.\n ';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
instructions ??= ((await response.json()) as { instructions: string }).instructions;
|
|
58
|
+
|
|
59
|
+
return instructions;
|
|
60
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { pino, type Level, type Logger } from 'pino';
|
|
4
|
+
import pretty from 'pino-pretty';
|
|
5
|
+
|
|
6
|
+
let _logger: Logger | undefined;
|
|
7
|
+
|
|
8
|
+
export function configureLogger({ level, pretty: usePretty }: { level: Level; pretty: boolean }): void {
|
|
9
|
+
_logger = pino(
|
|
10
|
+
{
|
|
11
|
+
level,
|
|
12
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
13
|
+
formatters: {
|
|
14
|
+
level(label) {
|
|
15
|
+
return { level: label };
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
usePretty ? pretty({ colorize: true, levelFirst: true, destination: 2 }) : process.stderr,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getLogger(): Logger {
|
|
24
|
+
if (!_logger) {
|
|
25
|
+
throw new Error('Logger has not been configured. Call configureLogger() before using the logger.');
|
|
26
|
+
}
|
|
27
|
+
return _logger;
|
|
28
|
+
}
|
package/src/methods.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
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.brand.retrieve',
|
|
15
|
+
fullyQualifiedName: 'brand.retrieve',
|
|
16
|
+
httpMethod: 'get',
|
|
17
|
+
httpPath: '/brand/retrieve',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
clientCallName: 'client.brand.aiProduct',
|
|
21
|
+
fullyQualifiedName: 'brand.aiProduct',
|
|
22
|
+
httpMethod: 'post',
|
|
23
|
+
httpPath: '/brand/ai/product',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
clientCallName: 'client.brand.aiProducts',
|
|
27
|
+
fullyQualifiedName: 'brand.aiProducts',
|
|
28
|
+
httpMethod: 'post',
|
|
29
|
+
httpPath: '/brand/ai/products',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
clientCallName: 'client.brand.aiQuery',
|
|
33
|
+
fullyQualifiedName: 'brand.aiQuery',
|
|
34
|
+
httpMethod: 'post',
|
|
35
|
+
httpPath: '/brand/ai/query',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
clientCallName: 'client.brand.fonts',
|
|
39
|
+
fullyQualifiedName: 'brand.fonts',
|
|
40
|
+
httpMethod: 'get',
|
|
41
|
+
httpPath: '/brand/fonts',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
clientCallName: 'client.brand.identifyFromTransaction',
|
|
45
|
+
fullyQualifiedName: 'brand.identifyFromTransaction',
|
|
46
|
+
httpMethod: 'get',
|
|
47
|
+
httpPath: '/brand/transaction_identifier',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
clientCallName: 'client.brand.prefetch',
|
|
51
|
+
fullyQualifiedName: 'brand.prefetch',
|
|
52
|
+
httpMethod: 'post',
|
|
53
|
+
httpPath: '/brand/prefetch',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
clientCallName: 'client.brand.prefetchByEmail',
|
|
57
|
+
fullyQualifiedName: 'brand.prefetchByEmail',
|
|
58
|
+
httpMethod: 'post',
|
|
59
|
+
httpPath: '/brand/prefetch-by-email',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
clientCallName: 'client.brand.retrieveByEmail',
|
|
63
|
+
fullyQualifiedName: 'brand.retrieveByEmail',
|
|
64
|
+
httpMethod: 'get',
|
|
65
|
+
httpPath: '/brand/retrieve-by-email',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
clientCallName: 'client.brand.retrieveByIsin',
|
|
69
|
+
fullyQualifiedName: 'brand.retrieveByIsin',
|
|
70
|
+
httpMethod: 'get',
|
|
71
|
+
httpPath: '/brand/retrieve-by-isin',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
clientCallName: 'client.brand.retrieveByName',
|
|
75
|
+
fullyQualifiedName: 'brand.retrieveByName',
|
|
76
|
+
httpMethod: 'get',
|
|
77
|
+
httpPath: '/brand/retrieve-by-name',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
clientCallName: 'client.brand.retrieveByTicker',
|
|
81
|
+
fullyQualifiedName: 'brand.retrieveByTicker',
|
|
82
|
+
httpMethod: 'get',
|
|
83
|
+
httpPath: '/brand/retrieve-by-ticker',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
clientCallName: 'client.brand.retrieveNaics',
|
|
87
|
+
fullyQualifiedName: 'brand.retrieveNaics',
|
|
88
|
+
httpMethod: 'get',
|
|
89
|
+
httpPath: '/brand/naics',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
clientCallName: 'client.brand.retrieveSimplified',
|
|
93
|
+
fullyQualifiedName: 'brand.retrieveSimplified',
|
|
94
|
+
httpMethod: 'get',
|
|
95
|
+
httpPath: '/brand/retrieve-simplified',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
clientCallName: 'client.brand.screenshot',
|
|
99
|
+
fullyQualifiedName: 'brand.screenshot',
|
|
100
|
+
httpMethod: 'get',
|
|
101
|
+
httpPath: '/brand/screenshot',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
clientCallName: 'client.brand.styleguide',
|
|
105
|
+
fullyQualifiedName: 'brand.styleguide',
|
|
106
|
+
httpMethod: 'get',
|
|
107
|
+
httpPath: '/brand/styleguide',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
clientCallName: 'client.brand.webScrapeHTML',
|
|
111
|
+
fullyQualifiedName: 'brand.webScrapeHTML',
|
|
112
|
+
httpMethod: 'get',
|
|
113
|
+
httpPath: '/web/scrape/html',
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
clientCallName: 'client.brand.webScrapeImages',
|
|
117
|
+
fullyQualifiedName: 'brand.webScrapeImages',
|
|
118
|
+
httpMethod: 'get',
|
|
119
|
+
httpPath: '/web/scrape/images',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
clientCallName: 'client.brand.webScrapeMd',
|
|
123
|
+
fullyQualifiedName: 'brand.webScrapeMd',
|
|
124
|
+
httpMethod: 'get',
|
|
125
|
+
httpPath: '/web/scrape/markdown',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
clientCallName: 'client.brand.webScrapeSitemap',
|
|
129
|
+
fullyQualifiedName: 'brand.webScrapeSitemap',
|
|
130
|
+
httpMethod: 'get',
|
|
131
|
+
httpPath: '/web/scrape/sitemap',
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
function allowedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
136
|
+
if (!options) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let allowedMethods: SdkMethod[];
|
|
141
|
+
|
|
142
|
+
if (options.codeAllowHttpGets || options.codeAllowedMethods) {
|
|
143
|
+
// Start with nothing allowed and then add into it from options
|
|
144
|
+
let allowedMethodsSet = new Set<SdkMethod>();
|
|
145
|
+
|
|
146
|
+
if (options.codeAllowHttpGets) {
|
|
147
|
+
// Add all methods that map to an HTTP GET
|
|
148
|
+
sdkMethods
|
|
149
|
+
.filter((method) => method.httpMethod === 'get')
|
|
150
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (options.codeAllowedMethods) {
|
|
154
|
+
// Add all methods that match any of the allowed regexps
|
|
155
|
+
const allowedRegexps = options.codeAllowedMethods.map((pattern) => {
|
|
156
|
+
try {
|
|
157
|
+
return new RegExp(pattern);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Invalid regex pattern for allowed method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
sdkMethods
|
|
166
|
+
.filter((method) => allowedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)))
|
|
167
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
allowedMethods = Array.from(allowedMethodsSet);
|
|
171
|
+
} else {
|
|
172
|
+
// Start with everything allowed
|
|
173
|
+
allowedMethods = [...sdkMethods];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (options.codeBlockedMethods) {
|
|
177
|
+
// Filter down based on blocked regexps
|
|
178
|
+
const blockedRegexps = options.codeBlockedMethods.map((pattern) => {
|
|
179
|
+
try {
|
|
180
|
+
return new RegExp(pattern);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`Invalid regex pattern for blocked method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
allowedMethods = allowedMethods.filter(
|
|
189
|
+
(method) => !blockedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)),
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return allowedMethods;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function blockedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
197
|
+
const allowedMethods = allowedMethodsForCodeTool(options);
|
|
198
|
+
if (!allowedMethods) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const allowedSet = new Set(allowedMethods.map((method) => method.fullyQualifiedName));
|
|
203
|
+
|
|
204
|
+
// Return any methods that are not explicitly allowed
|
|
205
|
+
return sdkMethods.filter((method) => !allowedSet.has(method.fullyQualifiedName));
|
|
206
|
+
}
|