@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.
- 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/{headers.mjs → auth.js} +18 -2
- package/auth.js.map +1 -0
- package/{headers.js → auth.mjs} +13 -6
- package/auth.mjs.map +1 -0
- package/code-tool-paths.cjs +6 -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.map +1 -1
- package/code-tool-types.d.ts.map +1 -1
- 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 +345 -0
- package/code-tool-worker.js.map +1 -0
- package/code-tool-worker.mjs +340 -0
- package/code-tool-worker.mjs.map +1 -0
- package/code-tool.d.mts +8 -2
- package/code-tool.d.mts.map +1 -1
- package/code-tool.d.ts +8 -2
- package/code-tool.d.ts.map +1 -1
- package/code-tool.js +264 -42
- package/code-tool.js.map +1 -1
- package/code-tool.mjs +229 -43
- package/code-tool.mjs.map +1 -1
- package/docs-search-tool.d.mts +9 -3
- package/docs-search-tool.d.mts.map +1 -1
- package/docs-search-tool.d.ts +9 -3
- package/docs-search-tool.d.ts.map +1 -1
- package/docs-search-tool.js +27 -4
- package/docs-search-tool.js.map +1 -1
- package/docs-search-tool.mjs +27 -4
- package/docs-search-tool.mjs.map +1 -1
- package/http.d.mts +3 -5
- package/http.d.mts.map +1 -1
- package/http.d.ts +3 -5
- package/http.d.ts.map +1 -1
- package/http.js +66 -43
- package/http.js.map +1 -1
- package/http.mjs +66 -43
- package/http.mjs.map +1 -1
- package/index.js +13 -12
- package/index.js.map +1 -1
- package/index.mjs +13 -12
- package/index.mjs.map +1 -1
- 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 +56 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +53 -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.map +1 -1
- package/methods.d.ts.map +1 -1
- package/methods.js +1 -0
- package/methods.js.map +1 -1
- package/methods.mjs +1 -0
- package/methods.mjs.map +1 -1
- package/options.d.mts +5 -0
- package/options.d.mts.map +1 -1
- package/options.d.ts +5 -0
- package/options.d.ts.map +1 -1
- package/options.js +31 -0
- package/options.js.map +1 -1
- package/options.mjs +31 -0
- package/options.mjs.map +1 -1
- package/package.json +62 -15
- package/server.d.mts +8 -9
- package/server.d.mts.map +1 -1
- package/server.d.ts +8 -9
- package/server.d.ts.map +1 -1
- package/server.js +73 -75
- package/server.js.map +1 -1
- package/server.mjs +72 -71
- package/server.mjs.map +1 -1
- package/src/{headers.ts → auth.ts} +16 -1
- package/src/code-tool-paths.cts +3 -0
- package/src/code-tool-types.ts +1 -0
- package/src/code-tool-worker.ts +391 -0
- package/src/code-tool.ts +309 -56
- package/src/docs-search-tool.ts +46 -6
- package/src/http.ts +74 -45
- package/src/index.ts +15 -13
- package/src/instructions.ts +75 -0
- package/src/logger.ts +28 -0
- package/src/methods.ts +2 -0
- package/src/options.ts +43 -0
- package/src/server.ts +85 -92
- package/src/stdio.ts +4 -3
- package/src/types.ts +12 -4
- package/src/util.ts +25 -0
- package/stdio.d.mts.map +1 -1
- package/stdio.d.ts.map +1 -1
- package/stdio.js +4 -3
- package/stdio.js.map +1 -1
- package/stdio.mjs +4 -3
- package/stdio.mjs.map +1 -1
- package/types.d.mts +8 -1
- package/types.d.mts.map +1 -1
- package/types.d.ts +8 -1
- package/types.d.ts.map +1 -1
- package/types.js.map +1 -1
- package/types.mjs.map +1 -1
- package/util.d.mts +4 -0
- package/util.d.mts.map +1 -0
- package/util.d.ts +4 -0
- package/util.d.ts.map +1 -0
- package/util.js +30 -0
- package/util.js.map +1 -0
- package/util.mjs +24 -0
- package/util.mjs.map +1 -0
- package/headers.d.mts +0 -4
- package/headers.d.mts.map +0 -1
- package/headers.d.ts +0 -4
- package/headers.d.ts.map +0 -1
- package/headers.js.map +0 -1
- 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
|
-
|
|
15
|
-
|
|
16
|
-
selectedTools.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
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
|
|
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.
|
|
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
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
+
logLevel = 'debug';
|
|
128
132
|
break;
|
|
129
133
|
case 'info':
|
|
130
|
-
|
|
134
|
+
logLevel = 'info';
|
|
131
135
|
break;
|
|
132
136
|
case 'notice':
|
|
133
137
|
case 'warning':
|
|
134
|
-
|
|
138
|
+
logLevel = 'warn';
|
|
135
139
|
break;
|
|
136
140
|
case 'error':
|
|
137
|
-
|
|
141
|
+
logLevel = 'error';
|
|
138
142
|
break;
|
|
139
143
|
default:
|
|
140
|
-
|
|
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
|
-
|
|
153
|
-
|
|
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
|
|
167
|
-
|
|
168
|
-
args
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
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
|
|
46
|
-
client: LlamaCloud
|
|
47
|
-
|
|
48
|
-
|
|
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;
|
|
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;
|
|
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
|
-
|
|
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;
|
|
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
|
-
|
|
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;
|
|
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"}
|