🙆

MCPサーバーを作ってみよう!ENS<>ウォレットアドレス

に公開

自作MCPを作ってみたい!

とりあえずざーとMCPに関するZennを読み漁って理解したので、自分でもMCPサーバーを作って見たい!ということで今回はENSからウォレットアドレスを調べるMCPサーバーを作ってみようと思います。

ENSからウォレットアドレスを調べるMCPサーバーを作る

実際のコード

src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { ethers } from "ethers";

const server = new McpServer({
    name: "ens",
    version: "1.0.0",
    capabilities: {
        resources: {},
        tools: {},
    },
});

// Ethers provider (Ethereum mainnet)
const provider = new ethers.JsonRpcProvider("https://eth.llamarpc.com");

// ENS resolve tool
server.tool(
    "resolve-ens",
    "Resolve ENS name to Ethereum address",
    {
        name: z.string().min(3).describe("ENS name, e.g. vitalik.eth"),
    },
    async ({ name }) => {
        try {
            const address = await provider.resolveName(name);
            if (!address) {
                return {
                    content: [
                        {
                            type: "text",
                            text: `ENS name "${name}" could not be resolved to an address.`,
                        },
                    ],
                };
            }
            return {
                content: [
                    {
                        type: "text",
                        text: `ENS name "${name}" resolves to:\n\n${address}`,
                    },
                ],
            };
        } catch (err) {
            console.error("Error resolving ENS name:", err);
            return {
                content: [
                    {
                        type: "text",
                        text: `An error occurred while resolving ENS name "${name}".`,
                    },
                ],
            };
        }
    }
);

// Main
async function main() {
    const transport = new StdioServerTransport();
    await server.connect(transport);
    console.error("ENS MCP Server running on stdio");
}

main().catch((error) => {
    console.error("Fatal error in main():", error);
    process.exit(1);
});

Claudeの設定ファイル

claude_desktop_config.json
{
    "mcpServers": {
        "ens": {
            "command": "node",
            "args": ["/Users/***/build/index.js"]
        }
    }
}

実際に動かしてみる

実際に作って見た感想

思ったより簡単、あとAIに簡単に道具を渡すことができて楽しい

Discussion