ai-debug-local-mcp
Version:
🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh
162 lines • 5.45 kB
JavaScript
/**
* MCP-LangChain Bridge
*
* Allows MCP to leverage LangChain capabilities while remaining in control.
* LangChain becomes a tool that MCP uses, not the other way around.
*/
/**
* Bridge that allows MCP to use LangChain features as tools
*/
export class MCPLangChainBridge {
apiKeys;
chains = new Map();
memory = new Map();
constructor(apiKeys) {
this.apiKeys = apiKeys;
this.initializeChains();
}
/**
* Initialize pre-configured chains
*/
initializeChains() {
// These would use actual LangChain imports
// For now, we'll create a mock structure
// Planning chain (uses Gemini for cost efficiency)
this.chains.set('planning', {
model: 'gemini',
prompt: 'Create a test plan for: {input}',
memory: false
});
// Validation chain (uses GPT-4 for accuracy)
this.chains.set('validation', {
model: 'openai',
prompt: 'Validate this screenshot against criteria: {criteria}',
memory: true
});
// Analysis chain (uses Claude for complex reasoning)
this.chains.set('analysis', {
model: 'claude',
prompt: 'Analyze this error and suggest fixes: {error}',
memory: true
});
}
/**
* Execute a chain - called by MCP tools
*/
async executeChain(chainName, params) {
const chainConfig = this.chains.get(chainName);
if (!chainConfig) {
throw new Error(`Chain '${chainName}' not found`);
}
// In real implementation, this would:
// 1. Create appropriate LangChain LLM instance
// 2. Set up memory if needed
// 3. Execute the chain
// 4. Return results
// Mock implementation
return {
success: true,
chain: chainName,
model: chainConfig.model,
result: `Mock result from ${chainConfig.model}`,
tokensUsed: 100,
cost: this.estimateCost(chainConfig.model, 100)
};
}
/**
* Create MCP tools that wrap LangChain functionality
*/
createMCPTools() {
return [
{
name: 'llm_chain_execute',
description: 'Execute a LangChain chain',
execute: async (params) => {
return this.executeChain(params.chain, params.input);
}
},
{
name: 'llm_memory_store',
description: 'Store conversation context',
execute: async (params) => {
const { sessionId, key, value } = params;
if (!this.memory.has(sessionId)) {
this.memory.set(sessionId, new Map());
}
this.memory.get(sessionId).set(key, value);
return { success: true };
}
},
{
name: 'llm_memory_retrieve',
description: 'Retrieve conversation context',
execute: async (params) => {
const { sessionId, key } = params;
const sessionMemory = this.memory.get(sessionId);
if (!sessionMemory)
return { value: null };
return { value: sessionMemory.get(key) };
}
},
{
name: 'llm_analyze_with_chain',
description: 'Analyze content using appropriate chain',
execute: async (params) => {
const { type, content } = params;
// Select appropriate chain based on analysis type
let chainName = 'analysis';
if (type === 'plan')
chainName = 'planning';
if (type === 'validate')
chainName = 'validation';
return this.executeChain(chainName, { input: content });
}
}
];
}
/**
* Estimate cost for token usage
*/
estimateCost(model, tokens) {
const costs = {
openai: 0.005, // $5 per million tokens
gemini: 0, // Free tier
claude: 0.003 // $3 per million tokens
};
return (tokens / 1000000) * (costs[model] || 0);
}
/**
* Create a custom chain dynamically
*/
async createCustomChain(config) {
// In real implementation, this would create a LangChain instance
this.chains.set(config.name, {
model: config.model,
prompt: config.prompt,
tools: config.tools || []
});
}
}
/**
* Integration example for MCP server
*/
export function integrateLangChainWithMCP(server, apiKeys) {
const bridge = new MCPLangChainBridge(apiKeys);
const tools = bridge.createMCPTools();
// Register each LangChain tool as an MCP tool
tools.forEach(tool => {
server.registerTool({
name: tool.name,
description: tool.description,
inputSchema: {
type: 'object',
properties: {
// Dynamic based on tool
}
},
handler: tool.execute
});
});
return bridge;
}
//# sourceMappingURL=mcp-langchain-bridge.js.map