Code Samples

Build agents that connect to the Moly MCP server programmatically

Three ways to connect to Moly programmatically. These examples use the hosted server for demonstration. For production use with real transactions, run the CLI locally (npx @moly-mcp/lido --server).

Anthropic API (MCP Connector)

The simplest approach. Anthropic handles the MCP connection — no MCP client library needed.

TypeScript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const response = await client.beta.messages.create(
  {
    model: "claude-sonnet-4-5-20250929",
    max_tokens: 1024,
    mcp_servers: [
      {
        type: "url",
        url: "https://moly-lido.vercel.app/api/mcp",
        name: "moly",
      },
    ],
    messages: [
      {
        role: "user",
        content: "What's the stETH balance for 0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18?",
      },
    ],
    tools: [{ type: "mcp_toolset" }],
  },
  {
    betas: ["mcp-client-2025-11-20"],
  }
);

console.log(response.content);
💡
Use the update_settings tool to change mode or network mid-session without restarting.

Vercel AI SDK

Use the Vercel AI SDK's MCP client with the local CLI server.

TypeScript
import { generateText } from "ai";
import { anthropic } from "@ai-sdk/anthropic";
import { createMCPClient } from "ai";
import { Experimental_StdioMCPTransport } from "ai/mcp-stdio";

const mcpClient = await createMCPClient({
  transport: new Experimental_StdioMCPTransport({
    command: "npx",
    args: ["@moly-mcp/lido", "--server"],
  }),
});

const tools = await mcpClient.tools();

const { text } = await generateText({
  model: anthropic("claude-sonnet-4-5-20250929"),
  tools,
  maxSteps: 5,
  prompt: "Simulate staking 0.1 ETH and show me the gas estimate",
});

console.log(text);
await mcpClient.close();

MCP SDK (TypeScript)

Use the MCP SDK directly for full control over tool calls.

TypeScript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";

const transport = new StdioClientTransport({
  command: "npx",
  args: ["@moly-mcp/lido", "--server"],
});

const client = new Client({
  name: "my-agent",
  version: "1.0.0",
});
await client.connect(transport);

// List all available tools
const { tools } = await client.listTools();
console.log("Available tools:", tools.map((t) => t.name));

// Get stETH balance
const balance = await client.callTool({
  name: "get_balance",
  arguments: { address: "0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18" },
});
console.log("Balance:", balance.content);

// Simulate staking
const stake = await client.callTool({
  name: "stake_eth",
  arguments: { amount_eth: "0.1" },
});
console.log("Stake simulation:", stake.content);

await client.close();

Configure Mode & Network

Configuration is set via the setup wizard and stored in ~/.moly/config.json. You can also change settings at runtime:

Via MCP tool call
// Use the update_settings tool from your agent
await client.callTool({
  name: "update_settings",
  arguments: { mode: "live", network: "mainnet" },
});
SettingValuesDefault
modesimulation, livesimulation
networkhoodi, mainnethoodi
chain_scopeethereum, allethereum
Live mode with a configured wallet will broadcast real transactions. Always validate with simulation first.