Neo N3 MCP 服务器基于 Model Context Protocol (MCP) 协议实现,为区块链应用提供强大的功能支持。同时,该软件包配备的 HTTP 服务器,通过 RESTful API 接口将 Neo N3 功能对外暴露,极大地方便了开发者的使用。
npm install neo-mcp-server http-restify
创建 server.js 文件:
const MCP = require('neo-mcp-server');
const restify = require('http-restify');
// 初始化 MCP 服务器
const mcpServer = new MCP.Server({
network: 'test',
contextPath: '/context'
});
// 初始化 HTTP 服务器
const httpServer = restify.createServer();
// 挂载 MCP 相关路由
mcpServer.applyRoutes(httpServer);
// 启动服务器
httpServer.listen(3000, () => {
console.log('服务器已启动,监听地址:http://localhost:3000');
});
node server.js
MCP 协议允许应用通过订阅特定区块链事件,实现实时数据获取功能。其主要特点包括:
// 创建 MCP 实例并设置配置
const mcp = new MCP({
network: 'test', // 网络环境:main 或 test
contextPath: '/context' // 上下文路径,默认为 /context
});
// 启动服务器
await mcp.start();
// 订阅某个地址的余额变更事件
mcp.subscribe('address-balance-changed', (event) => {
console.log(`Address ${event.address} 的余额发生了变化,当前余额为 ${event.balance}`);
});
// 处理智能合约调用事件
mcp.on('contract-invoked', async (event) => {
try {
const result = await event.execute();
console.log(`Contract ${event.contract} 调用成功,返回结果:${result}`);
} catch (error) {
console.error(`Contract ${event.contract} 调用失败,错误信息:${error.message}`);
}
});
HTTP 服务器通过 RESTful API 提供以下功能:
| 端点 | 方法 | 描述 |
|---|---|---|
/api/context/current |
GET | 获取当前 MCP 上下文信息 |
/api/chains/main/info |
GET | 获取主链基本信息 |
/api/blocks/height |
GET | 查询当前网络高度 |
/api/transactions/hash |
GET | 根据哈希获取交易详情 |
# 获取区块链信息
curl http://localhost:3000/api/context/current
# 部署智能合约
curl -X POST http://localhost:3000/api/contracts \
-H "Content-Type: application/json" \
-d '{"contractName": "MyContract", "code":"..."}'
# 调用智能合约
curl -X POST http://localhost:3000/api/contracts/MyContract/call \
-H "Content-Type: application/json" \
-d '{"method":"balanceOf","params":["userAddress"]}'
如有任何问题或建议,请联系我们的技术支持团队:support@neonetwork.org