Skip to Content
🤖 AI AgentsCustom Agents

Custom Agents

Use @embarkai/mcp programmatically in your own AI agent or application.

Using as a library

The package exports createMcpServer and startMcpServer functions that you can use to embed the MCP server in your own process.

Start with stdio transport

The simplest approach — start the server and let it communicate over stdin/stdout:

import { startMcpServer } from '@embarkai/mcp'; await startMcpServer({ apiKey: process.env.EMBARK_API_KEY!, walletId: process.env.EMBARK_WALLET_ID!, chainId: 2030232745, keyshareDir: './data/keyshares', keysharePassword: process.env.EMBARK_KEYSHARE_PASSWORD, walletBackupPassword: process.env.EMBARK_WALLET_BACKUP_PASSWORD, debug: false, });

You can also call startMcpServer() without arguments to use the default resolveConfig(), which reads from environment variables.

Create server with custom transport

For more control, use createMcpServer to get the McpServer instance and connect your own transport:

import { createMcpServer, resolveConfig } from '@embarkai/mcp'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; const config = resolveConfig(); // reads from env vars const server = await createMcpServer(config); // Connect to stdio (or any other transport) const transport = new StdioServerTransport(); await server.connect(transport);

Running as a subprocess

The most common pattern for custom agents is to spawn the MCP server as a child process and communicate over stdio using the @modelcontextprotocol/sdk client.

Install dependencies

npm install @modelcontextprotocol/sdk @embarkai/mcp

Client example

import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; // Spawn the MCP server as a child process const transport = new StdioClientTransport({ command: 'npx', args: ['-y', '@embarkai/mcp'], env: { ...process.env, EMBARK_API_KEY: 'your_api_key', EMBARK_WALLET_ID: 'your_wallet_id', EMBARK_CHAIN_ID: '2030232745', }, }); const client = new Client( { name: 'my-agent', version: '1.0.0' }, { capabilities: {} }, ); await client.connect(transport); // List available tools const { tools } = await client.listTools(); console.log('Available tools:', tools.map(t => t.name)); // Call a tool const result = await client.callTool({ name: 'get_wallet_info', arguments: {}, }); console.log('Wallet info:', result.content); // Check balance const balance = await client.callTool({ name: 'get_balance', arguments: {}, }); console.log('Balance:', balance.content); // Transfer tokens const transfer = await client.callTool({ name: 'transfer', arguments: { to: '0xRecipientAddress', amount: '0.1', }, }); console.log('Transfer result:', transfer.content); // Clean up await client.close();

Integrating with LLM frameworks

With LangChain

If your agent framework supports MCP tools, pass the tool definitions from client.listTools() to your LLM chain. The MCP SDK handles serialization and transport.

import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; // Set up the MCP client (as shown above) const transport = new StdioClientTransport({ command: 'npx', args: ['-y', '@embarkai/mcp'], env: { ...process.env, EMBARK_API_KEY: process.env.EMBARK_API_KEY!, EMBARK_WALLET_ID: process.env.EMBARK_WALLET_ID!, }, }); const client = new Client( { name: 'langchain-agent', version: '1.0.0' }, { capabilities: {} }, ); await client.connect(transport); // Get tool definitions for your LLM const { tools } = await client.listTools(); // Convert MCP tools to your framework's tool format // and use client.callTool() as the execution handler

Configuration reference

The McpConfig interface accepted by createMcpServer:

interface McpConfig { apiKey: string; // EmbarkAI API key (required) walletId: string; // Wallet identifier (required) chainId: number; // Initial chain ID (default: 2030232745) keyshareDir: string; // Keyshare storage directory keysharePassword?: string; // Encryption password for file-based storage walletBackupPassword?: string; // Password for vault backup restore debug: boolean; // Enable debug logging }

See Setup & Installation for details on each configuration option.

Last updated on