Monad MCP 教程
该项目演示了如何创建与 Monad 测试网交互的 MCP 服务器。MCP 服务器提供了一个用于检查 Monad 测试网上 MON 代币余额的工具。
什么是 MCP?
模型上下文协议 (MCP) 是一种允许 AI 模型与外部工具和服务交互的标准。
在本教程中,我们将创建一个 MCP 服务器,允许 MCP 客户端(Claude Desktop)查询 Monad 测试网以检查帐户的 MON 余额。
先决条件
- Node.js(v16 或更高版本)
npm
或yarn
- 克劳德桌面
入门
- 克隆此存储库
git clone https://github.com/bble/monad-mcp.git
- 安装依赖项:
构建 MCP 服务器
Monad Testnet 相关配置已经添加到src
文件夹中的index.ts
。
定义服务器实例
// Create a new MCP server instance
const server = new McpServer({
name: "monad-testnet",
version: "0.0.1",
// Array of supported tool names that clients can call
capabilities: ["get-mon-balance"]
});
定义 MON 余额工具
server.tool(
// Tool ID
"get-mon-balance",
// Description of what the tool does
"Get MON balance for an address on Monad testnet",
// Input schema
{
address: z.string().describe("Monad testnet address to check balance for"),
},
// Tool implementation
async ({ address }) => {
try {
// Check MON balance for the input address
const balance = await publicClient.getBalance({
address: address as `0x${string}`,
});
// Return a human friendly message indicating the balance.
return {
content: [
{
type: "text",
text: `Balance for ${address}: ${formatUnits(balance, 18)} MON`,
},
],
};
} catch (error) {
// If the balance check process fails, return a graceful message back to the MCP client indicating a failure.
return {
content: [
{
type: "text",
text: `Failed to retrieve balance for address: ${address}. Error: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
}
);
添加查询 NFT 数量的功能
### Add functionality to query NFT count
server.tool(
// Function identifier
"get-nft-count",
// Function description
"Query the number of NFTs held by an address on the Monad testnet",
// Parameter definition
{
address: z.string().describe("The Monad testnet address to query"),
nftContract: z.string().describe("NFT contract address")
},
// Function implementation
async ({ address, nftContract }) => {
try {
// Call the contract's balanceOf method to get the NFT count
const balance = await publicClient.readContract({
address: nftContract as `0x${string}`,
abi: [
{
inputs: [{ name: "owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "", type: "uint256" }],
stateMutability: "view",
type: "function"
}
],
functionName: "balanceOf",
args: [address as `0x${string}`]
});
// Return the formatted query result
return {
content: [
{
type: "text",
text: `The address ${address} holds ${balance.toString()} NFTs in contract ${nftContract}.`,
},
],
};
} catch (error) {
// Error handling
return {
content: [
{
type: "text",
text: `Failed to query NFT count for address ${address}: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
}
);
从main
函数初始化传输和服务器
async function main() {
// Create a transport layer using standard input/output
const transport = new StdioServerTransport();
// Connect the server to the transport
await server.connect(transport);
console.error("Monad testnet MCP Server running on stdio");
}
构建项目
服务器现在可以使用了!
将 MCP 服务器添加到 Claude Desktop
- 打开“克劳德桌面”
- 打开“设置”
Claude > 设置 > 开发者
- 打开
claude_desktop_config.json
- 添加有关 MCP 服务器的详细信息并保存文件。
{
"mcpServers": {
...
"monad-mcp": {
"command": "node",
"args": [
"/<path-to-project>/build/index.js"
]
}
}
}
- 重启“Claude Desktop”
使用 MCP 服务器
这是最终结果
更多资源