LangChain Integration
Wrap Bithaven as LangChain-compatible tools for OpenAI or any LLM.
Setup
npm install @bithaven/mcp-sdk langchain @langchain/openaiCreate LangChain Tools
Wrap the BithavenClient methods as DynamicTool instances:
import { BithavenClient, PolicyDeniedError } from '@bithaven/mcp-sdk';
import { DynamicTool } from 'langchain/tools';
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
import { ChatOpenAI } from '@langchain/openai';
const bithaven = new BithavenClient({
apiKey: process.env.BITHAVEN_API_KEY!,
});
const tools = [
new DynamicTool({
name: 'bithaven_check_balance',
description: 'Check USDC balance and remaining spend caps.',
func: async () => JSON.stringify(await bithaven.checkBalance()),
}),
new DynamicTool({
name: 'bithaven_send_payment',
description:
'Send USDC. Input: JSON { toAddress, amount, memo? }',
func: async (input: string) => {
try {
const params = JSON.parse(input);
return JSON.stringify(await bithaven.sendPayment(params));
} catch (err) {
if (err instanceof PolicyDeniedError) {
return JSON.stringify({
error: 'Denied by policy',
violations: err.violations,
});
}
return JSON.stringify({ error: String(err) });
}
},
}),
new DynamicTool({
name: 'bithaven_get_tx_history',
description: 'Get transaction history. Input: JSON { page?, limit? }',
func: async (input: string) => {
const params = input ? JSON.parse(input) : {};
return JSON.stringify(await bithaven.getTxHistory(params));
},
}),
new DynamicTool({
name: 'bithaven_request_approval',
description:
'Request human approval. Input: JSON { toAddress, amount, memo?, reason? }',
func: async (input: string) => {
const params = JSON.parse(input);
return JSON.stringify(await bithaven.requestApproval(params));
},
}),
];
// Create and run the agent
const model = new ChatOpenAI({ modelName: 'gpt-4o', temperature: 0 });
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: 'openai-functions',
verbose: true,
});
const result = await executor.invoke({
input: 'Check my balance, then send 5 USDC to 0x742d...2bD68',
});
console.log(result.output);Environment Variables
BITHAVEN_API_KEY=bh_live_...
OPENAI_API_KEY=sk-...