Skip to content

Commit fdfe246

Browse files
fix: graph state and config cache (#21)
1 parent 07f1725 commit fdfe246

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

packages/mcp/mcps.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
"args": ["-y", "@kasarlabs/ask-starknet-help-mcp@latest"],
440440
"transport": "stdio"
441441
},
442-
"description": "Help and documentation for Ask Starknet: usage guide, architecture explanation, capabilities listing, project ideas, and troubleshooting",
442+
"description": "Help and documentation for Ask Starknet: usage guide, architecture explanation, capabilities listing like MCPs and tools, project ideas, and troubleshooting",
443443
"promptInfo": {
444444
"expertise": "Ask Starknet help, usage guide, documentation, architecture explanation, available MCPs and tools, project ideas, and troubleshooting",
445445
"tools": [

packages/mcp/mcps.local.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,22 @@
432432
"expertise": "Cairo smart contract development, Starknet technical documentation, ecosystem knowledge, and recent Starknet news",
433433
"tools": ["assist_with_cairo", "starknet_general_knowledge"]
434434
}
435+
},
436+
"ask-starknet-help": {
437+
"client": {
438+
"command": "npx",
439+
"args": ["-y", "@kasarlabs/ask-starknet-help-mcp@latest"],
440+
"transport": "stdio"
441+
},
442+
"description": "Help and documentation for Ask Starknet: usage guide, architecture explanation, capabilities listing, project ideas, and troubleshooting",
443+
"promptInfo": {
444+
"expertise": "Ask Starknet help, usage guide, documentation, architecture explanation, available MCPs and tools, project ideas, and troubleshooting",
445+
"tools": [
446+
"ask_starknet_help",
447+
"ask_starknet_explain_architecture",
448+
"ask_starknet_list_capabilities",
449+
"ask_starknet_suggest_projects"
450+
]
451+
}
435452
}
436453
}

packages/mcp/src/graph/agents/selector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Respond with the exact name of the chosen agent or "__end__".`;
7373
next: END,
7474
messages: [
7575
new AIMessage({
76-
content: `I couldn't find an appropriate specialized agent to handle this request: "${userInput}"\n\nReasoning: ${response.reasoning}\n\nPlease try rephrasing your request or use one of the available capabilities. You can ask "help" to see what I can do.`,
76+
content: `I couldn't find an appropriate specialized agent to handle this request: "${userInput}"\n\nPlease try rephrasing your request or use one of the available capabilities. You can ask "help" to see what I can do.`,
7777
name: 'selector-error',
7878
}),
7979
],

packages/mcp/src/graph/mcps/utilities.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import { logger } from '../../utils/logger.js';
1212
const __filename = fileURLToPath(import.meta.url);
1313
const __dirname = dirname(__filename);
1414

15+
// Cache for loaded configuration
16+
let cachedConfig: Record<string, MCPServerInfo> | null = null;
17+
1518
function loadMcpsConfig(): Record<string, MCPServerInfo> {
19+
if (cachedConfig) {
20+
return cachedConfig;
21+
}
22+
1623
try {
1724
// mcps.json is at the root of the package
1825
// From build/graph/mcps/utilities.js -> ../../../mcps.json
@@ -22,7 +29,9 @@ function loadMcpsConfig(): Record<string, MCPServerInfo> {
2229
}
2330
logger.info('Loading mcps.json from path:', configPath);
2431
const configContent = readFileSync(configPath, 'utf-8');
25-
return JSON.parse(configContent);
32+
const config: Record<string, MCPServerInfo> = JSON.parse(configContent);
33+
cachedConfig = config;
34+
return config;
2635
} catch (error) {
2736
console.error('Error loading mcps.json:', error);
2837
console.error('Tried path:', join(__dirname, '../../../mcps.json'));
@@ -75,9 +84,7 @@ export const getMCPClientConfig = (
7584

7685
if (missingVars.length > 0) {
7786
throw new Error(
78-
`Missing environment variables for MCP '${serverName}': ${missingVars.join(', ')}\n` +
79-
`Available variables: ${Object.keys(env).join(', ')}\n` +
80-
`Required variables: ${Object.keys(serverInfo.client.env).join(', ')}`
87+
`Missing environment variables for MCP '${serverName}': ${missingVars.join(', ')}\n`
8188
);
8289
}
8390
}

packages/mcp/src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import packageJson from '../package.json' with { type: 'json' };
99
import { graph } from './graph/graph.js';
1010
import { logger } from './utils/logger.js';
1111
import { HumanMessage } from '@langchain/core/messages';
12+
import { getAllMcpInfo } from './graph/mcps/utilities.js';
13+
import type { MCPServerInfo } from './graph/mcps/interfaces.js';
1214

1315
const performStarknetActionsSchema = z.object({
1416
userInput: z.string().describe('The actions that the user want to do'),
@@ -141,7 +143,20 @@ function validateRequiredEnvironmentVariables(): envInput {
141143
}
142144
}
143145

144-
Object.keys(process.env).forEach((key) => {
146+
// Build whitelist from mcps.json - only collect env vars needed by MCPs
147+
const mcpsConfig = getAllMcpInfo();
148+
const allowedEnvVars = new Set<string>();
149+
150+
Object.values(mcpsConfig).forEach((mcpInfo: MCPServerInfo) => {
151+
if (mcpInfo.client.env) {
152+
Object.keys(mcpInfo.client.env).forEach((envVar) => {
153+
allowedEnvVars.add(envVar);
154+
});
155+
}
156+
});
157+
158+
// Only collect whitelisted environment variables
159+
allowedEnvVars.forEach((key) => {
145160
if (process.env[key] && !env[key]) {
146161
env[key] = process.env[key];
147162
}

0 commit comments

Comments
 (0)