@llamaindex/llama-cloud-mcp 1.5.0 → 1.7.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 (131) hide show
  1. package/auth.d.mts +6 -0
  2. package/auth.d.mts.map +1 -0
  3. package/auth.d.ts +6 -0
  4. package/auth.d.ts.map +1 -0
  5. package/{headers.mjs → auth.js} +18 -2
  6. package/auth.js.map +1 -0
  7. package/{headers.js → auth.mjs} +13 -6
  8. package/auth.mjs.map +1 -0
  9. package/code-tool-paths.cjs +6 -0
  10. package/code-tool-paths.cjs.map +1 -0
  11. package/code-tool-paths.d.cts +2 -0
  12. package/code-tool-paths.d.cts.map +1 -0
  13. package/code-tool-types.d.mts.map +1 -1
  14. package/code-tool-types.d.ts.map +1 -1
  15. package/code-tool-worker.d.mts +5 -0
  16. package/code-tool-worker.d.mts.map +1 -0
  17. package/code-tool-worker.d.ts +5 -0
  18. package/code-tool-worker.d.ts.map +1 -0
  19. package/code-tool-worker.js +345 -0
  20. package/code-tool-worker.js.map +1 -0
  21. package/code-tool-worker.mjs +340 -0
  22. package/code-tool-worker.mjs.map +1 -0
  23. package/code-tool.d.mts +8 -2
  24. package/code-tool.d.mts.map +1 -1
  25. package/code-tool.d.ts +8 -2
  26. package/code-tool.d.ts.map +1 -1
  27. package/code-tool.js +264 -42
  28. package/code-tool.js.map +1 -1
  29. package/code-tool.mjs +229 -43
  30. package/code-tool.mjs.map +1 -1
  31. package/docs-search-tool.d.mts +9 -3
  32. package/docs-search-tool.d.mts.map +1 -1
  33. package/docs-search-tool.d.ts +9 -3
  34. package/docs-search-tool.d.ts.map +1 -1
  35. package/docs-search-tool.js +27 -4
  36. package/docs-search-tool.js.map +1 -1
  37. package/docs-search-tool.mjs +27 -4
  38. package/docs-search-tool.mjs.map +1 -1
  39. package/http.d.mts +3 -5
  40. package/http.d.mts.map +1 -1
  41. package/http.d.ts +3 -5
  42. package/http.d.ts.map +1 -1
  43. package/http.js +66 -43
  44. package/http.js.map +1 -1
  45. package/http.mjs +66 -43
  46. package/http.mjs.map +1 -1
  47. package/index.js +13 -12
  48. package/index.js.map +1 -1
  49. package/index.mjs +13 -12
  50. package/index.mjs.map +1 -1
  51. package/instructions.d.mts +2 -0
  52. package/instructions.d.mts.map +1 -0
  53. package/instructions.d.ts +2 -0
  54. package/instructions.d.ts.map +1 -0
  55. package/instructions.js +56 -0
  56. package/instructions.js.map +1 -0
  57. package/instructions.mjs +53 -0
  58. package/instructions.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 +1 -0
  70. package/methods.js.map +1 -1
  71. package/methods.mjs +1 -0
  72. package/methods.mjs.map +1 -1
  73. package/options.d.mts +5 -0
  74. package/options.d.mts.map +1 -1
  75. package/options.d.ts +5 -0
  76. package/options.d.ts.map +1 -1
  77. package/options.js +31 -0
  78. package/options.js.map +1 -1
  79. package/options.mjs +31 -0
  80. package/options.mjs.map +1 -1
  81. package/package.json +62 -15
  82. package/server.d.mts +8 -9
  83. package/server.d.mts.map +1 -1
  84. package/server.d.ts +8 -9
  85. package/server.d.ts.map +1 -1
  86. package/server.js +73 -75
  87. package/server.js.map +1 -1
  88. package/server.mjs +72 -71
  89. package/server.mjs.map +1 -1
  90. package/src/{headers.ts → auth.ts} +16 -1
  91. package/src/code-tool-paths.cts +3 -0
  92. package/src/code-tool-types.ts +1 -0
  93. package/src/code-tool-worker.ts +391 -0
  94. package/src/code-tool.ts +309 -56
  95. package/src/docs-search-tool.ts +46 -6
  96. package/src/http.ts +74 -45
  97. package/src/index.ts +15 -13
  98. package/src/instructions.ts +75 -0
  99. package/src/logger.ts +28 -0
  100. package/src/methods.ts +2 -0
  101. package/src/options.ts +43 -0
  102. package/src/server.ts +85 -92
  103. package/src/stdio.ts +4 -3
  104. package/src/types.ts +12 -4
  105. package/src/util.ts +25 -0
  106. package/stdio.d.mts.map +1 -1
  107. package/stdio.d.ts.map +1 -1
  108. package/stdio.js +4 -3
  109. package/stdio.js.map +1 -1
  110. package/stdio.mjs +4 -3
  111. package/stdio.mjs.map +1 -1
  112. package/types.d.mts +8 -1
  113. package/types.d.mts.map +1 -1
  114. package/types.d.ts +8 -1
  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.d.mts +4 -0
  119. package/util.d.mts.map +1 -0
  120. package/util.d.ts +4 -0
  121. package/util.d.ts.map +1 -0
  122. package/util.js +30 -0
  123. package/util.js.map +1 -0
  124. package/util.mjs +24 -0
  125. package/util.mjs.map +1 -0
  126. package/headers.d.mts +0 -4
  127. package/headers.d.mts.map +0 -1
  128. package/headers.d.ts +0 -4
  129. package/headers.d.ts.map +0 -1
  130. package/headers.js.map +0 -1
  131. package/headers.mjs.map +0 -1
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,75 @@
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
+ // Periodically evict stale entries so the cache doesn't grow unboundedly.
16
+ const _cacheCleanupInterval = setInterval(() => {
17
+ const now = Date.now();
18
+ for (const [key, entry] of instructionsCache) {
19
+ if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
20
+ instructionsCache.delete(key);
21
+ }
22
+ }
23
+ }, INSTRUCTIONS_CACHE_TTL_MS);
24
+
25
+ // Don't keep the process alive just for cleanup.
26
+ _cacheCleanupInterval.unref();
27
+
28
+ export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
29
+ const cacheKey = stainlessApiKey ?? '';
30
+ const cached = instructionsCache.get(cacheKey);
31
+
32
+ if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
33
+ return cached.fetchedInstructions;
34
+ }
35
+
36
+ const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
37
+ instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() });
38
+ return fetchedInstructions;
39
+ }
40
+
41
+ async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
42
+ // Setting the stainless API key is optional, but may be required
43
+ // to authenticate requests to the Stainless API.
44
+ const response = await fetch(
45
+ readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/llamacloud-prod',
46
+ {
47
+ method: 'GET',
48
+ headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
49
+ },
50
+ );
51
+
52
+ let instructions: string | undefined;
53
+ if (!response.ok) {
54
+ getLogger().warn(
55
+ 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
56
+ );
57
+
58
+ instructions = `
59
+ This is the llamacloud-prod MCP server. You will use Code Mode to help the user perform
60
+ actions. You can use search_docs tool to learn about how to take action with this server. Then,
61
+ you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
62
+ thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
63
+ block: it can be as long as you need to get the job done!
64
+ `;
65
+ }
66
+
67
+ instructions ??= ((await response.json()) as { instructions: string }).instructions;
68
+ instructions = `
69
+ If needed, you can get the current time by executing Date.now().
70
+
71
+ ${instructions}
72
+ `;
73
+
74
+ return instructions;
75
+ }
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 CHANGED
@@ -1,3 +1,5 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
1
3
  import { McpOptions } from './options';
2
4
 
3
5
  export type SdkMethod = {
package/src/options.ts CHANGED
@@ -1,22 +1,31 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
1
3
  import qs from 'qs';
2
4
  import yargs from 'yargs';
3
5
  import { hideBin } from 'yargs/helpers';
4
6
  import z from 'zod';
7
+ import { readEnv } from './util';
5
8
 
6
9
  export type CLIOptions = McpOptions & {
7
10
  debug: boolean;
11
+ logFormat: 'json' | 'pretty';
8
12
  transport: 'stdio' | 'http';
9
13
  port: number | undefined;
10
14
  socket: string | undefined;
11
15
  };
12
16
 
13
17
  export type McpOptions = {
18
+ includeCodeTool?: boolean | undefined;
14
19
  includeDocsTools?: boolean | undefined;
20
+ stainlessApiKey?: string | undefined;
15
21
  codeAllowHttpGets?: boolean | undefined;
16
22
  codeAllowedMethods?: string[] | undefined;
17
23
  codeBlockedMethods?: string[] | undefined;
24
+ codeExecutionMode: McpCodeExecutionMode;
18
25
  };
19
26
 
27
+ export type McpCodeExecutionMode = 'stainless-sandbox' | 'local';
28
+
20
29
  export function parseCLIOptions(): CLIOptions {
21
30
  const opts = yargs(hideBin(process.argv))
22
31
  .option('code-allow-http-gets', {
@@ -36,7 +45,19 @@ export function parseCLIOptions(): CLIOptions {
36
45
  description:
37
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.',
38
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
+ })
39
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
+ })
40
61
  .option('no-tools', {
41
62
  type: 'string',
42
63
  array: true,
@@ -49,6 +70,12 @@ export function parseCLIOptions(): CLIOptions {
49
70
  description: 'Port to serve on if using http transport',
50
71
  })
51
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
+ })
52
79
  .option('tools', {
53
80
  type: 'string',
54
81
  array: true,
@@ -72,17 +99,26 @@ export function parseCLIOptions(): CLIOptions {
72
99
  : argv.tools?.includes(toolType) ? true
73
100
  : undefined;
74
101
 
102
+ const includeCodeTool = shouldIncludeToolType('code');
75
103
  const includeDocsTools = shouldIncludeToolType('docs');
76
104
 
77
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';
78
110
 
79
111
  return {
112
+ ...(includeCodeTool !== undefined && { includeCodeTool }),
80
113
  ...(includeDocsTools !== undefined && { includeDocsTools }),
81
114
  debug: !!argv.debug,
115
+ stainlessApiKey: argv.stainlessApiKey,
82
116
  codeAllowHttpGets: argv.codeAllowHttpGets,
83
117
  codeAllowedMethods: argv.codeAllowedMethods,
84
118
  codeBlockedMethods: argv.codeBlockedMethods,
119
+ codeExecutionMode: argv.codeExecutionMode as McpCodeExecutionMode,
85
120
  transport,
121
+ logFormat,
86
122
  port: argv.port,
87
123
  socket: argv.socket,
88
124
  };
@@ -107,12 +143,19 @@ export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): M
107
143
  const queryObject = typeof query === 'string' ? qs.parse(query) : query;
108
144
  const queryOptions = QueryOptions.parse(queryObject);
109
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
+
110
151
  let docsTools: boolean | undefined =
111
152
  queryOptions.no_tools && queryOptions.no_tools?.includes('docs') ? false
112
153
  : queryOptions.tools?.includes('docs') ? true
113
154
  : defaultOptions.includeDocsTools;
114
155
 
115
156
  return {
157
+ ...(codeTool !== undefined && { includeCodeTool: codeTool }),
116
158
  ...(docsTools !== undefined && { includeDocsTools: docsTools }),
159
+ codeExecutionMode: defaultOptions.codeExecutionMode,
117
160
  };
118
161
  }
package/src/server.ts CHANGED
@@ -11,57 +11,19 @@ import { ClientOptions } from '@llamaindex/llama-cloud';
11
11
  import LlamaCloud from '@llamaindex/llama-cloud';
12
12
  import { codeTool } from './code-tool';
13
13
  import docsSearchTool from './docs-search-tool';
14
+ import { getInstructions } from './instructions';
14
15
  import { McpOptions } from './options';
15
16
  import { blockedMethodsForCodeTool } from './methods';
16
- import { HandlerFunction, McpTool } from './types';
17
+ import { HandlerFunction, McpRequestContext, ToolCallResult, McpTool } from './types';
17
18
 
18
- export { McpOptions } from './options';
19
- export { ClientOptions } from '@llamaindex/llama-cloud';
20
-
21
- async function getInstructions() {
22
- // This API key is optional; providing it allows the server to fetch instructions for unreleased versions.
23
- const stainlessAPIKey = readEnv('STAINLESS_API_KEY');
24
- const response = await fetch(
25
- readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/llamacloud-prod',
26
- {
27
- method: 'GET',
28
- headers: { ...(stainlessAPIKey && { Authorization: stainlessAPIKey }) },
29
- },
30
- );
31
-
32
- let instructions: string | undefined;
33
- if (!response.ok) {
34
- console.warn(
35
- 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
36
- );
37
-
38
- instructions = `
39
- This is the llamacloud-prod MCP server. You will use Code Mode to help the user perform
40
- actions. You can use search_docs tool to learn about how to take action with this server. Then,
41
- you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
42
- thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
43
- block: it can be as long as you need to get the job done!
44
- `;
45
- }
46
-
47
- instructions ??= ((await response.json()) as { instructions: string }).instructions;
48
- instructions = `
49
- The current time in Unix timestamps is ${Date.now()}.
50
-
51
- ${instructions}
52
- `;
53
-
54
- return instructions;
55
- }
56
-
57
- export const newMcpServer = async () =>
19
+ export const newMcpServer = async (stainlessApiKey: string | undefined) =>
58
20
  new McpServer(
59
21
  {
60
22
  name: 'llamaindex_llama_cloud_api',
61
- version: '1.5.0',
23
+ version: '1.7.0',
62
24
  },
63
25
  {
64
- instructions: await getInstructions(),
26
+ instructions: await getInstructions(stainlessApiKey),
65
27
  capabilities: { tools: {}, logging: {} },
66
28
  },
67
29
  );
@@ -74,6 +36,7 @@ export async function initMcpServer(params: {
74
36
  server: Server | McpServer;
75
37
  clientOptions?: ClientOptions;
76
38
  mcpOptions?: McpOptions;
39
+ stainlessApiKey?: string | undefined;
77
40
  }) {
78
41
  const server = params.server instanceof McpServer ? params.server.server : params.server;
79
42
 
@@ -92,14 +55,32 @@ export async function initMcpServer(params: {
92
55
  error: logAtLevel('error'),
93
56
  };
94
57
 
95
- let client = new LlamaCloud({
96
- logger,
97
- ...params.clientOptions,
98
- defaultHeaders: {
99
- ...params.clientOptions?.defaultHeaders,
100
- 'X-Stainless-MCP': 'true',
101
- },
102
- });
58
+ let _client: LlamaCloud | undefined;
59
+ let _clientError: Error | undefined;
60
+ let _logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off' | undefined;
61
+
62
+ const getClient = (): LlamaCloud => {
63
+ if (_clientError) throw _clientError;
64
+ if (!_client) {
65
+ try {
66
+ _client = new LlamaCloud({
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
+ };
103
84
 
104
85
  const providedTools = selectTools(params.mcpOptions);
105
86
  const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
@@ -117,29 +98,56 @@ export async function initMcpServer(params: {
117
98
  throw new Error(`Unknown tool: ${name}`);
118
99
  }
119
100
 
120
- return executeHandler(mcpTool.handler, client, args);
101
+ let client: LlamaCloud;
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
+ });
121
124
  });
122
125
 
123
126
  server.setRequestHandler(SetLevelRequestSchema, async (request) => {
124
127
  const { level } = request.params;
128
+ let logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off';
125
129
  switch (level) {
126
130
  case 'debug':
127
- client = client.withOptions({ logLevel: 'debug' });
131
+ logLevel = 'debug';
128
132
  break;
129
133
  case 'info':
130
- client = client.withOptions({ logLevel: 'info' });
134
+ logLevel = 'info';
131
135
  break;
132
136
  case 'notice':
133
137
  case 'warning':
134
- client = client.withOptions({ logLevel: 'warn' });
138
+ logLevel = 'warn';
135
139
  break;
136
140
  case 'error':
137
- client = client.withOptions({ logLevel: 'error' });
141
+ logLevel = 'error';
138
142
  break;
139
143
  default:
140
- client = client.withOptions({ logLevel: 'off' });
144
+ logLevel = 'off';
141
145
  break;
142
146
  }
147
+ _logLevel = logLevel;
148
+ if (_client) {
149
+ _client = _client.withOptions({ logLevel });
150
+ }
143
151
  return {};
144
152
  });
145
153
  }
@@ -148,11 +156,16 @@ export async function initMcpServer(params: {
148
156
  * Selects the tools to include in the MCP Server based on the provided options.
149
157
  */
150
158
  export function selectTools(options?: McpOptions): McpTool[] {
151
- const includedTools = [
152
- codeTool({
153
- blockedMethods: blockedMethodsForCodeTool(options),
154
- }),
155
- ];
159
+ const includedTools = [];
160
+
161
+ if (options?.includeCodeTool ?? true) {
162
+ includedTools.push(
163
+ codeTool({
164
+ blockedMethods: blockedMethodsForCodeTool(options),
165
+ codeExecutionMode: options?.codeExecutionMode ?? 'stainless-sandbox',
166
+ }),
167
+ );
168
+ }
156
169
  if (options?.includeDocsTools ?? true) {
157
170
  includedTools.push(docsSearchTool);
158
171
  }
@@ -162,34 +175,14 @@ export function selectTools(options?: McpOptions): McpTool[] {
162
175
  /**
163
176
  * Runs the provided handler with the given client and arguments.
164
177
  */
165
- export async function executeHandler(
166
- handler: HandlerFunction,
167
- client: LlamaCloud,
168
- args: Record<string, unknown> | undefined,
169
- ) {
170
- return await handler(client, args || {});
178
+ export async function executeHandler({
179
+ handler,
180
+ reqContext,
181
+ args,
182
+ }: {
183
+ handler: HandlerFunction;
184
+ reqContext: McpRequestContext;
185
+ args: Record<string, unknown> | undefined;
186
+ }): Promise<ToolCallResult> {
187
+ return await handler({ reqContext, args: args || {} });
171
188
  }
172
-
173
- export const readEnv = (env: string): string | undefined => {
174
- if (typeof (globalThis as any).process !== 'undefined') {
175
- return (globalThis as any).process.env?.[env]?.trim();
176
- } else if (typeof (globalThis as any).Deno !== 'undefined') {
177
- return (globalThis as any).Deno.env?.get?.(env)?.trim();
178
- }
179
- return;
180
- };
181
-
182
- export const readEnvOrError = (env: string): string => {
183
- let envValue = readEnv(env);
184
- if (envValue === undefined) {
185
- throw new Error(`Environment variable ${env} is not set`);
186
- }
187
- return envValue;
188
- };
189
-
190
- export const requireValue = <T>(value: T | undefined, description: string): T => {
191
- if (value === undefined) {
192
- throw new Error(`Missing required value: ${description}`);
193
- }
194
- return value;
195
- };
package/src/stdio.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
2
2
  import { McpOptions } from './options';
3
3
  import { initMcpServer, newMcpServer } from './server';
4
+ import { getLogger } from './logger';
4
5
 
5
6
  export const launchStdioServer = async (mcpOptions: McpOptions) => {
6
- const server = await newMcpServer();
7
+ const server = await newMcpServer(mcpOptions.stainlessApiKey);
7
8
 
8
- await initMcpServer({ server, mcpOptions });
9
+ await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
9
10
 
10
11
  const transport = new StdioServerTransport();
11
12
  await server.connect(transport);
12
- console.error('MCP Server running on stdio');
13
+ getLogger().info('MCP Server running on stdio');
13
14
  };
package/src/types.ts CHANGED
@@ -42,10 +42,18 @@ export type ToolCallResult = {
42
42
  isError?: boolean;
43
43
  };
44
44
 
45
- export type HandlerFunction = (
46
- client: LlamaCloud,
47
- args: Record<string, unknown> | undefined,
48
- ) => Promise<ToolCallResult>;
45
+ export type McpRequestContext = {
46
+ client: LlamaCloud;
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>;
49
57
 
50
58
  export function asTextContentResult(result: unknown): ToolCallResult {
51
59
  return {
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.map CHANGED
@@ -1 +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"}
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.map CHANGED
@@ -1 +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"}
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 CHANGED
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.launchStdioServer = void 0;
4
4
  const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
5
5
  const server_1 = require("./server.js");
6
+ const logger_1 = require("./logger.js");
6
7
  const launchStdioServer = async (mcpOptions) => {
7
- const server = await (0, server_1.newMcpServer)();
8
- await (0, server_1.initMcpServer)({ server, mcpOptions });
8
+ const server = await (0, server_1.newMcpServer)(mcpOptions.stainlessApiKey);
9
+ await (0, server_1.initMcpServer)({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
9
10
  const transport = new stdio_js_1.StdioServerTransport();
10
11
  await server.connect(transport);
11
- console.error('MCP Server running on stdio');
12
+ (0, logger_1.getLogger)().info('MCP Server running on stdio');
12
13
  };
13
14
  exports.launchStdioServer = launchStdioServer;
14
15
  //# sourceMappingURL=stdio.js.map
package/stdio.js.map CHANGED
@@ -1 +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,GAAE,CAAC;IAEpC,MAAM,IAAA,sBAAa,EAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAE5C,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"}
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 CHANGED
@@ -1,10 +1,11 @@
1
1
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
2
2
  import { initMcpServer, newMcpServer } from "./server.mjs";
3
+ import { getLogger } from "./logger.mjs";
3
4
  export const launchStdioServer = async (mcpOptions) => {
4
- const server = await newMcpServer();
5
- await initMcpServer({ server, mcpOptions });
5
+ const server = await newMcpServer(mcpOptions.stainlessApiKey);
6
+ await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
6
7
  const transport = new StdioServerTransport();
7
8
  await server.connect(transport);
8
- console.error('MCP Server running on stdio');
9
+ getLogger().info('MCP Server running on stdio');
9
10
  };
10
11
  //# sourceMappingURL=stdio.mjs.map
package/stdio.mjs.map CHANGED
@@ -1 +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;AAEtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,UAAsB,EAAE,EAAE;IAChE,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IAEpC,MAAM,aAAa,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAE5C,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;AAC/C,CAAC,CAAC"}
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"}