@stigg/typescript-mcp 0.1.0-alpha.9 → 0.1.0-beta.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.
Files changed (121) hide show
  1. package/code-tool-paths.cjs +8 -0
  2. package/code-tool-paths.cjs.map +1 -0
  3. package/code-tool-paths.d.cts +2 -0
  4. package/code-tool-paths.d.cts.map +1 -0
  5. package/code-tool-types.d.mts.map +1 -1
  6. package/code-tool-types.d.ts.map +1 -1
  7. package/code-tool-worker.d.mts +5 -0
  8. package/code-tool-worker.d.mts.map +1 -0
  9. package/code-tool-worker.d.ts +5 -0
  10. package/code-tool-worker.d.ts.map +1 -0
  11. package/code-tool-worker.js +357 -0
  12. package/code-tool-worker.js.map +1 -0
  13. package/code-tool-worker.mjs +319 -0
  14. package/code-tool-worker.mjs.map +1 -0
  15. package/code-tool.d.mts +8 -2
  16. package/code-tool.d.mts.map +1 -1
  17. package/code-tool.d.ts +8 -2
  18. package/code-tool.d.ts.map +1 -1
  19. package/code-tool.js +266 -37
  20. package/code-tool.js.map +1 -1
  21. package/code-tool.mjs +233 -37
  22. package/code-tool.mjs.map +1 -1
  23. package/docs-search-tool.d.mts +3 -1
  24. package/docs-search-tool.d.mts.map +1 -1
  25. package/docs-search-tool.d.ts +3 -1
  26. package/docs-search-tool.d.ts.map +1 -1
  27. package/docs-search-tool.js +53 -4
  28. package/docs-search-tool.js.map +1 -1
  29. package/docs-search-tool.mjs +52 -4
  30. package/docs-search-tool.mjs.map +1 -1
  31. package/http.d.mts +2 -4
  32. package/http.d.mts.map +1 -1
  33. package/http.d.ts +2 -4
  34. package/http.d.ts.map +1 -1
  35. package/http.js +123 -40
  36. package/http.js.map +1 -1
  37. package/http.mjs +123 -40
  38. package/http.mjs.map +1 -1
  39. package/index.js +13 -12
  40. package/index.js.map +1 -1
  41. package/index.mjs +13 -12
  42. package/index.mjs.map +1 -1
  43. package/instructions.d.mts +5 -0
  44. package/instructions.d.mts.map +1 -0
  45. package/instructions.d.ts +5 -0
  46. package/instructions.d.ts.map +1 -0
  47. package/instructions.js +61 -0
  48. package/instructions.js.map +1 -0
  49. package/instructions.mjs +55 -0
  50. package/instructions.mjs.map +1 -0
  51. package/local-docs-search.d.mts +28 -0
  52. package/local-docs-search.d.mts.map +1 -0
  53. package/local-docs-search.d.ts +28 -0
  54. package/local-docs-search.d.ts.map +1 -0
  55. package/local-docs-search.js +4774 -0
  56. package/local-docs-search.js.map +1 -0
  57. package/local-docs-search.mjs +4734 -0
  58. package/local-docs-search.mjs.map +1 -0
  59. package/logger.d.mts +7 -0
  60. package/logger.d.mts.map +1 -0
  61. package/logger.d.ts +7 -0
  62. package/logger.d.ts.map +1 -0
  63. package/logger.js +29 -0
  64. package/logger.js.map +1 -0
  65. package/logger.mjs +22 -0
  66. package/logger.mjs.map +1 -0
  67. package/methods.d.mts.map +1 -1
  68. package/methods.d.ts.map +1 -1
  69. package/methods.js +235 -43
  70. package/methods.js.map +1 -1
  71. package/methods.mjs +235 -43
  72. package/methods.mjs.map +1 -1
  73. package/options.d.mts +7 -0
  74. package/options.d.mts.map +1 -1
  75. package/options.d.ts +7 -0
  76. package/options.d.ts.map +1 -1
  77. package/options.js +42 -0
  78. package/options.js.map +1 -1
  79. package/options.mjs +42 -0
  80. package/options.mjs.map +1 -1
  81. package/package.json +58 -7
  82. package/server.d.mts +10 -1
  83. package/server.d.mts.map +1 -1
  84. package/server.d.ts +10 -1
  85. package/server.d.ts.map +1 -1
  86. package/server.js +72 -47
  87. package/server.js.map +1 -1
  88. package/server.mjs +72 -47
  89. package/server.mjs.map +1 -1
  90. package/src/code-tool-paths.cts +5 -0
  91. package/src/code-tool-types.ts +1 -0
  92. package/src/code-tool-worker.ts +370 -0
  93. package/src/code-tool.ts +302 -47
  94. package/src/docs-search-tool.ts +81 -11
  95. package/src/http.ts +131 -42
  96. package/src/index.ts +15 -13
  97. package/src/instructions.ts +83 -0
  98. package/src/local-docs-search.ts +5667 -0
  99. package/src/logger.ts +28 -0
  100. package/src/methods.ts +235 -43
  101. package/src/options.ts +56 -0
  102. package/src/server.ts +85 -58
  103. package/src/stdio.ts +6 -2
  104. package/src/types.ts +3 -0
  105. package/src/util.ts +2 -2
  106. package/stdio.d.mts.map +1 -1
  107. package/stdio.d.ts.map +1 -1
  108. package/stdio.js +6 -2
  109. package/stdio.js.map +1 -1
  110. package/stdio.mjs +6 -2
  111. package/stdio.mjs.map +1 -1
  112. package/types.d.mts +6 -0
  113. package/types.d.mts.map +1 -1
  114. package/types.d.ts +6 -0
  115. package/types.d.ts.map +1 -1
  116. package/types.js.map +1 -1
  117. package/types.mjs.map +1 -1
  118. package/util.js +2 -2
  119. package/util.js.map +1 -1
  120. package/util.mjs +2 -2
  121. package/util.mjs.map +1 -1
package/src/http.ts CHANGED
@@ -4,9 +4,10 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
4
4
  import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
5
5
  import { ClientOptions } from '@stigg/typescript';
6
6
  import express from 'express';
7
- import morgan from 'morgan';
8
- import morganBody from 'morgan-body';
7
+ import pino from 'pino';
8
+ import pinoHttp from 'pino-http';
9
9
  import { getStainlessApiKey, parseClientAuthHeaders } from './auth';
10
+ import { getLogger } from './logger';
10
11
  import { McpOptions } from './options';
11
12
  import { initMcpServer, newMcpServer } from './server';
12
13
 
@@ -22,29 +23,72 @@ const newServer = async ({
22
23
  res: express.Response;
23
24
  }): Promise<McpServer | null> => {
24
25
  const stainlessApiKey = getStainlessApiKey(req, mcpOptions);
25
- const server = await newMcpServer(stainlessApiKey);
26
+ const customInstructionsPath = mcpOptions.customInstructionsPath;
27
+ const server = await newMcpServer({ stainlessApiKey, customInstructionsPath });
26
28
 
27
- try {
28
- const authOptions = parseClientAuthHeaders(req, false);
29
+ const authOptions = parseClientAuthHeaders(req, false);
29
30
 
30
- await initMcpServer({
31
- server: server,
32
- mcpOptions: mcpOptions,
33
- clientOptions: {
34
- ...clientOptions,
35
- ...authOptions,
36
- },
37
- stainlessApiKey: stainlessApiKey,
38
- });
39
- } catch (error) {
40
- res.status(401).json({
41
- jsonrpc: '2.0',
42
- error: {
43
- code: -32000,
44
- message: `Unauthorized: ${error instanceof Error ? error.message : error}`,
45
- },
46
- });
47
- return null;
31
+ let upstreamClientEnvs: Record<string, string> | undefined;
32
+ const clientEnvsHeader = req.headers['x-stainless-mcp-client-envs'];
33
+ if (typeof clientEnvsHeader === 'string') {
34
+ try {
35
+ const parsed = JSON.parse(clientEnvsHeader);
36
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
37
+ upstreamClientEnvs = parsed;
38
+ }
39
+ } catch {
40
+ // Ignore malformed header
41
+ }
42
+ }
43
+
44
+ // Parse x-stainless-mcp-client-permissions header to override permission options
45
+ //
46
+ // Note: Permissions are best-effort and intended to prevent clients from doing unexpected things;
47
+ // they're not a hard security boundary, so we allow arbitrary, client-driven overrides.
48
+ //
49
+ // See the Stainless MCP documentation for more details.
50
+ let effectiveMcpOptions = mcpOptions;
51
+ const clientPermissionsHeader = req.headers['x-stainless-mcp-client-permissions'];
52
+ if (typeof clientPermissionsHeader === 'string') {
53
+ try {
54
+ const parsed = JSON.parse(clientPermissionsHeader);
55
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
56
+ effectiveMcpOptions = {
57
+ ...mcpOptions,
58
+ ...(typeof parsed.allow_http_gets === 'boolean' && { codeAllowHttpGets: parsed.allow_http_gets }),
59
+ ...(Array.isArray(parsed.allowed_methods) && { codeAllowedMethods: parsed.allowed_methods }),
60
+ ...(Array.isArray(parsed.blocked_methods) && { codeBlockedMethods: parsed.blocked_methods }),
61
+ };
62
+ getLogger().info(
63
+ { clientPermissions: parsed },
64
+ 'Overriding code execution permissions from x-stainless-mcp-client-permissions header',
65
+ );
66
+ }
67
+ } catch (error) {
68
+ getLogger().warn({ error }, 'Failed to parse x-stainless-mcp-client-permissions header');
69
+ }
70
+ }
71
+
72
+ const mcpClientInfo =
73
+ typeof req.body?.params?.clientInfo?.name === 'string' ?
74
+ { name: req.body.params.clientInfo.name, version: String(req.body.params.clientInfo.version ?? '') }
75
+ : undefined;
76
+
77
+ await initMcpServer({
78
+ server: server,
79
+ mcpOptions: effectiveMcpOptions,
80
+ clientOptions: {
81
+ ...clientOptions,
82
+ ...authOptions,
83
+ },
84
+ stainlessApiKey: stainlessApiKey,
85
+ upstreamClientEnvs,
86
+ mcpSessionId: (req as any).mcpSessionId,
87
+ mcpClientInfo,
88
+ });
89
+
90
+ if (mcpClientInfo) {
91
+ getLogger().info({ mcpSessionId: (req as any).mcpSessionId, mcpClientInfo }, 'MCP client connected');
48
92
  }
49
93
 
50
94
  return server;
@@ -81,29 +125,74 @@ const del = async (req: express.Request, res: express.Response) => {
81
125
  });
82
126
  };
83
127
 
128
+ const redactHeaders = (headers: Record<string, any>) => {
129
+ const hiddenHeaders = /auth|cookie|key|token|x-stainless-mcp-client-envs/i;
130
+ const filtered = { ...headers };
131
+ Object.keys(filtered).forEach((key) => {
132
+ if (hiddenHeaders.test(key)) {
133
+ filtered[key] = '[REDACTED]';
134
+ }
135
+ });
136
+ return filtered;
137
+ };
138
+
84
139
  export const streamableHTTPApp = ({
85
140
  clientOptions = {},
86
141
  mcpOptions,
87
- debug,
88
142
  }: {
89
143
  clientOptions?: ClientOptions;
90
144
  mcpOptions: McpOptions;
91
- debug: boolean;
92
145
  }): express.Express => {
93
146
  const app = express();
94
147
  app.set('query parser', 'extended');
95
148
  app.use(express.json());
96
-
97
- if (debug) {
98
- morganBody(app, {
99
- logAllReqHeader: true,
100
- logAllResHeader: true,
101
- logRequestBody: true,
102
- logResponseBody: true,
103
- });
104
- } else {
105
- app.use(morgan('combined'));
106
- }
149
+ app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
150
+ const existing = req.headers['mcp-session-id'];
151
+ const sessionId = (Array.isArray(existing) ? existing[0] : existing) || crypto.randomUUID();
152
+ (req as any).mcpSessionId = sessionId;
153
+ const origWriteHead = res.writeHead.bind(res);
154
+ res.writeHead = function (statusCode: number, ...rest: any[]) {
155
+ res.setHeader('mcp-session-id', sessionId);
156
+ return origWriteHead(statusCode, ...rest);
157
+ } as typeof res.writeHead;
158
+ next();
159
+ });
160
+ app.use(
161
+ pinoHttp({
162
+ logger: getLogger(),
163
+ customProps: (req) => ({
164
+ mcpSessionId: (req as any).mcpSessionId,
165
+ }),
166
+ customLogLevel: (req, res) => {
167
+ if (res.statusCode >= 500) {
168
+ return 'error';
169
+ } else if (res.statusCode >= 400) {
170
+ return 'warn';
171
+ }
172
+ return 'info';
173
+ },
174
+ customSuccessMessage: function (req, res) {
175
+ return `Request ${req.method} to ${req.url} completed with status ${res.statusCode}`;
176
+ },
177
+ customErrorMessage: function (req, res, err) {
178
+ return `Request ${req.method} to ${req.url} errored with status ${res.statusCode}`;
179
+ },
180
+ serializers: {
181
+ req: pino.stdSerializers.wrapRequestSerializer((req) => {
182
+ return {
183
+ ...req,
184
+ headers: redactHeaders(req.raw.headers),
185
+ };
186
+ }),
187
+ res: pino.stdSerializers.wrapResponseSerializer((res) => {
188
+ return {
189
+ ...res,
190
+ headers: redactHeaders(res.headers),
191
+ };
192
+ }),
193
+ },
194
+ }),
195
+ );
107
196
 
108
197
  app.get('/health', async (req: express.Request, res: express.Response) => {
109
198
  res.status(200).send('OK');
@@ -117,22 +206,22 @@ export const streamableHTTPApp = ({
117
206
 
118
207
  export const launchStreamableHTTPServer = async ({
119
208
  mcpOptions,
120
- debug,
121
209
  port,
122
210
  }: {
123
211
  mcpOptions: McpOptions;
124
- debug: boolean;
125
212
  port: number | string | undefined;
126
213
  }) => {
127
- const app = streamableHTTPApp({ mcpOptions, debug });
214
+ const app = streamableHTTPApp({ mcpOptions });
128
215
  const server = app.listen(port);
129
216
  const address = server.address();
130
217
 
218
+ const logger = getLogger();
219
+
131
220
  if (typeof address === 'string') {
132
- console.error(`MCP Server running on streamable HTTP at ${address}`);
221
+ logger.info(`MCP Server running on streamable HTTP at ${address}`);
133
222
  } else if (address !== null) {
134
- console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
223
+ logger.info(`MCP Server running on streamable HTTP on port ${address.port}`);
135
224
  } else {
136
- console.error(`MCP Server running on streamable HTTP on port ${port}`);
225
+ logger.info(`MCP Server running on streamable HTTP on port ${port}`);
137
226
  }
138
227
  };
package/src/index.ts CHANGED
@@ -5,15 +5,20 @@ import { McpOptions, parseCLIOptions } from './options';
5
5
  import { launchStdioServer } from './stdio';
6
6
  import { launchStreamableHTTPServer } from './http';
7
7
  import type { McpTool } from './types';
8
+ import { configureLogger, getLogger } from './logger';
8
9
 
9
10
  async function main() {
10
11
  const options = parseOptionsOrError();
12
+ configureLogger({
13
+ level: options.debug ? 'debug' : 'info',
14
+ pretty: options.logFormat === 'pretty',
15
+ });
11
16
 
12
17
  const selectedTools = await selectToolsOrError(options);
13
18
 
14
- console.error(
15
- `MCP Server starting with ${selectedTools.length} tools:`,
16
- selectedTools.map((e) => e.tool.name),
19
+ getLogger().info(
20
+ { tools: selectedTools.map((e) => e.tool.name) },
21
+ `MCP Server starting with ${selectedTools.length} tools`,
17
22
  );
18
23
 
19
24
  switch (options.transport) {
@@ -23,8 +28,7 @@ async function main() {
23
28
  case 'http':
24
29
  await launchStreamableHTTPServer({
25
30
  mcpOptions: options,
26
- debug: options.debug,
27
- port: options.port ?? options.socket,
31
+ port: options.socket ?? options.port,
28
32
  });
29
33
  break;
30
34
  }
@@ -32,7 +36,8 @@ async function main() {
32
36
 
33
37
  if (require.main === module) {
34
38
  main().catch((error) => {
35
- console.error('Fatal error in main():', error);
39
+ // Logger might not be initialized yet
40
+ console.error('Fatal error in main()', error);
36
41
  process.exit(1);
37
42
  });
38
43
  }
@@ -41,7 +46,8 @@ function parseOptionsOrError() {
41
46
  try {
42
47
  return parseCLIOptions();
43
48
  } catch (error) {
44
- console.error('Error parsing options:', error);
49
+ // Logger is initialized after options, so use console.error here
50
+ console.error('Error parsing options', error);
45
51
  process.exit(1);
46
52
  }
47
53
  }
@@ -50,16 +56,12 @@ async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
50
56
  try {
51
57
  const includedTools = selectTools(options);
52
58
  if (includedTools.length === 0) {
53
- console.error('No tools match the provided filters.');
59
+ getLogger().error('No tools match the provided filters');
54
60
  process.exit(1);
55
61
  }
56
62
  return includedTools;
57
63
  } catch (error) {
58
- if (error instanceof Error) {
59
- console.error('Error filtering tools:', error.message);
60
- } else {
61
- console.error('Error filtering tools:', error);
62
- }
64
+ getLogger().error({ error }, 'Error filtering tools');
63
65
  process.exit(1);
64
66
  }
65
67
  }
@@ -0,0 +1,83 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import fs from 'fs/promises';
4
+ import { getLogger } from './logger';
5
+ import { readEnv } from './util';
6
+
7
+ const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
8
+
9
+ interface InstructionsCacheEntry {
10
+ fetchedInstructions: string;
11
+ fetchedAt: number;
12
+ }
13
+
14
+ const instructionsCache = new Map<string, InstructionsCacheEntry>();
15
+
16
+ export async function getInstructions({
17
+ stainlessApiKey,
18
+ customInstructionsPath,
19
+ }: {
20
+ stainlessApiKey?: string | undefined;
21
+ customInstructionsPath?: string | undefined;
22
+ }): Promise<string> {
23
+ const now = Date.now();
24
+ const cacheKey = customInstructionsPath ?? stainlessApiKey ?? '';
25
+ const cached = instructionsCache.get(cacheKey);
26
+
27
+ if (cached && now - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
28
+ return cached.fetchedInstructions;
29
+ }
30
+
31
+ // Evict stale entries so the cache doesn't grow unboundedly.
32
+ for (const [key, entry] of instructionsCache) {
33
+ if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
34
+ instructionsCache.delete(key);
35
+ }
36
+ }
37
+
38
+ let fetchedInstructions: string;
39
+
40
+ if (customInstructionsPath) {
41
+ fetchedInstructions = await fetchLatestInstructionsFromFile(customInstructionsPath);
42
+ } else {
43
+ fetchedInstructions = await fetchLatestInstructionsFromApi(stainlessApiKey);
44
+ }
45
+
46
+ instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: now });
47
+ return fetchedInstructions;
48
+ }
49
+
50
+ async function fetchLatestInstructionsFromFile(path: string): Promise<string> {
51
+ try {
52
+ return await fs.readFile(path, 'utf-8');
53
+ } catch (error) {
54
+ getLogger().error({ error, path }, 'Error fetching instructions from file');
55
+ throw error;
56
+ }
57
+ }
58
+
59
+ async function fetchLatestInstructionsFromApi(stainlessApiKey: string | undefined): Promise<string> {
60
+ // Setting the stainless API key is optional, but may be required
61
+ // to authenticate requests to the Stainless API.
62
+ const response = await fetch(
63
+ readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/stigg',
64
+ {
65
+ method: 'GET',
66
+ headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
67
+ },
68
+ );
69
+
70
+ let instructions: string | undefined;
71
+ if (!response.ok) {
72
+ getLogger().warn(
73
+ 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
74
+ );
75
+
76
+ instructions =
77
+ '\n This is the stigg 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 ';
78
+ }
79
+
80
+ instructions ??= ((await response.json()) as { instructions: string }).instructions;
81
+
82
+ return instructions;
83
+ }