|
| 1 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 2 | +import { getMcpTools } from "@coinbase/agentkit-model-context-protocol"; |
| 3 | +import { |
| 4 | + AgentKit, |
| 5 | + CdpWalletProvider, |
| 6 | + wethActionProvider, |
| 7 | + walletActionProvider, |
| 8 | + erc20ActionProvider, |
| 9 | + erc721ActionProvider, |
| 10 | + cdpApiActionProvider, |
| 11 | + cdpWalletActionProvider, |
| 12 | + pythActionProvider, |
| 13 | +} from "@coinbase/agentkit"; |
| 14 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 15 | +import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Validates that required environment variables are set |
| 19 | + */ |
| 20 | +function validateEnvironment(): void { |
| 21 | + const requiredVars = ["CDP_API_KEY_NAME", "CDP_API_KEY_PRIVATE_KEY"]; |
| 22 | + const missingVars = requiredVars.filter(varName => !process.env[varName]); |
| 23 | + |
| 24 | + if (missingVars.length > 0) { |
| 25 | + console.error("Error: Required environment variables are not set:"); |
| 26 | + missingVars.forEach(varName => { |
| 27 | + console.error(`${varName}=your_${varName.toLowerCase()}_here`); |
| 28 | + }); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + if (!process.env.NETWORK_ID) { |
| 33 | + console.warn("Warning: NETWORK_ID not set, defaulting to base-sepolia testnet"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * This function creates a new server instance with the capabilities to handle MCP requests. |
| 39 | + * It configures the CDP Wallet Provider with the provided API keys and network ID. |
| 40 | + * It then initializes the AgentKit with the configured wallet provider and action providers. |
| 41 | + * |
| 42 | + * @returns {Promise<Server>} The initialized MCP server |
| 43 | + */ |
| 44 | +async function initializeServer() { |
| 45 | + try { |
| 46 | + // Create server instance with capabilities |
| 47 | + const server = new Server( |
| 48 | + { |
| 49 | + name: "cdp-agentkit", |
| 50 | + version: "1.0.0", |
| 51 | + }, |
| 52 | + { |
| 53 | + capabilities: { |
| 54 | + tools: {}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + ); |
| 58 | + |
| 59 | + // Configure CDP Wallet Provider |
| 60 | + const config = { |
| 61 | + apiKeyName: process.env.CDP_API_KEY_NAME!, |
| 62 | + apiKeyPrivateKey: process.env.CDP_API_KEY_PRIVATE_KEY!, |
| 63 | + networkId: process.env.NETWORK_ID || "base-sepolia", |
| 64 | + }; |
| 65 | + |
| 66 | + const walletProvider = await CdpWalletProvider.configureWithWallet(config); |
| 67 | + |
| 68 | + // Initialize AgentKit |
| 69 | + const agentkit = await AgentKit.from({ |
| 70 | + walletProvider, |
| 71 | + actionProviders: [ |
| 72 | + wethActionProvider(), |
| 73 | + pythActionProvider(), |
| 74 | + walletActionProvider(), |
| 75 | + erc20ActionProvider(), |
| 76 | + erc721ActionProvider(), |
| 77 | + cdpApiActionProvider({ |
| 78 | + apiKeyName: config.apiKeyName, |
| 79 | + apiKeyPrivateKey: config.apiKeyPrivateKey, |
| 80 | + }), |
| 81 | + cdpWalletActionProvider({ |
| 82 | + apiKeyName: config.apiKeyName, |
| 83 | + apiKeyPrivateKey: config.apiKeyPrivateKey, |
| 84 | + }), |
| 85 | + ], |
| 86 | + }); |
| 87 | + |
| 88 | + // Get MCP tools from AgentKit |
| 89 | + const { tools, toolHandler } = await getMcpTools(agentkit); |
| 90 | + |
| 91 | + // Set up request handlers |
| 92 | + server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 93 | + return { |
| 94 | + tools, |
| 95 | + }; |
| 96 | + }); |
| 97 | + |
| 98 | + server.setRequestHandler(CallToolRequestSchema, async request => { |
| 99 | + try { |
| 100 | + return await toolHandler(request.params.name, request.params.arguments); |
| 101 | + } catch (error) { |
| 102 | + console.error(`Error executing tool ${request.params.name}:`, error); |
| 103 | + throw new Error(`Tool ${request.params.name} failed: ${error}`); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return server; |
| 108 | + } catch (error) { |
| 109 | + console.error("Failed to initialize server:", error); |
| 110 | + throw error; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Main function to run the MCP server |
| 116 | + */ |
| 117 | +async function main() { |
| 118 | + validateEnvironment(); |
| 119 | + |
| 120 | + try { |
| 121 | + const server = await initializeServer(); |
| 122 | + const transport = new StdioServerTransport(); |
| 123 | + await server.connect(transport); |
| 124 | + console.error("CDP AgentKit MCP Server running on stdio"); |
| 125 | + } catch (error) { |
| 126 | + console.error("Fatal error:", error); |
| 127 | + process.exit(1); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +if (require.main === module) { |
| 132 | + main().catch(error => { |
| 133 | + console.error("Fatal error in main():", error); |
| 134 | + process.exit(1); |
| 135 | + }); |
| 136 | +} |
0 commit comments