Skip to content

Commit 0dc1bfc

Browse files
authored
feat: add polkadot agent kit mcp server example (#103)
1 parent f003f81 commit 0dc1bfc

File tree

9 files changed

+381
-35
lines changed

9 files changed

+381
-35
lines changed

eslint.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default tseslint.config(
2929
},
3030
languageOptions: {
3131
parserOptions: {
32-
project: ["./packages/*/tsconfig*.json"],
32+
project: ["./packages/*/tsconfig*.json", "./examples/*/tsconfig.json"],
3333
tsconfigRootDir: import.meta.dirname,
3434
},
3535
},
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"arrowParens": "avoid",
3+
"bracketSpacing": true,
4+
"semi": false,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"trailingComma": "none"
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"mcpServers": {
3+
"agentkit": {
4+
"command": "node",
5+
"args": [
6+
"/Users/YOUR-USERNAME/polkadot-agent-kit/examples/mcp-server/dist/index.js"
7+
],
8+
"env": {
9+
"PRIVATE_KEY": "YOUR PRIVATE_KEY"
10+
11+
}
12+
}
13+
}
14+
}

examples/mcp-server/index.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3+
import {
4+
CallToolRequestSchema,
5+
ListToolsRequestSchema,
6+
} from "@modelcontextprotocol/sdk/types.js";
7+
import { PolkadotAgentKit, getMcpTools } from "@polkadot-agent-kit/sdk";
8+
9+
10+
function validateEnvironment(): void {
11+
const requiredVars = ["PRIVATE_KEY"];
12+
const missingVars = requiredVars.filter(varName => !process.env[varName]);
13+
14+
if (missingVars.length > 0) {
15+
const errorMessage = `Missing required environment variables: ${missingVars.join(', ')}`;
16+
throw new Error(errorMessage);
17+
}
18+
}
19+
20+
21+
22+
async function initializeServer() {
23+
try {
24+
// Create MCP server
25+
const server = new Server(
26+
{
27+
name: "polkadot-agent-kit",
28+
version: "1.0.0",
29+
},
30+
{
31+
capabilities: {
32+
tools: {},
33+
},
34+
}
35+
);
36+
37+
// Initialize the agent kit
38+
const polkadotAgentKit = new PolkadotAgentKit({
39+
privateKey: process.env.PRIVATE_KEY,
40+
41+
});
42+
43+
await polkadotAgentKit.initializeApi();
44+
45+
// Get MCP tools
46+
const { tools, toolHandler } = await getMcpTools(polkadotAgentKit);
47+
48+
// Handle tool listing
49+
server.setRequestHandler(ListToolsRequestSchema, async () => {
50+
return { tools };
51+
});
52+
53+
// Handle tool calls
54+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
55+
return await toolHandler(request.params.name, request.params.arguments);
56+
});
57+
58+
return server;
59+
} catch (error) {
60+
console.error("Failed to initialize server:", error);
61+
throw error;
62+
}
63+
}
64+
65+
/**
66+
* Main function to run the Polkadot MCP server
67+
*/
68+
async function main() {
69+
try {
70+
validateEnvironment();
71+
const server = await initializeServer();
72+
const transport = new StdioServerTransport();
73+
await server.connect(transport);
74+
console.error("Polkadot Agent Kit MCP Server running on stdio");
75+
} catch (error) {
76+
console.error("Fatal error:", error);
77+
process.exit(1);
78+
}
79+
}
80+
81+
if (require.main === module) {
82+
main().catch(error => {
83+
console.error("Fatal error in main():", error);
84+
process.exit(1);
85+
});
86+
}

examples/mcp-server/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "Polkadot Agent Kit MCP Server",
3+
"description": "Example Model Context Protocol Server for Polkadot Agent Kit",
4+
"version": "1.0.0",
5+
"private": true,
6+
"author": "Polkadot Agent Kit",
7+
"license": "Apache-2.0",
8+
"scripts": {
9+
"build": "tsc",
10+
"start": "node dist/index.js",
11+
"dev": "pnpm build && pnpm start"
12+
},
13+
"dependencies": {
14+
"@polkadot-agent-kit/sdk": "workspace:*",
15+
"@modelcontextprotocol/sdk": "^1.6.1",
16+
"zod": "^3.22.4"
17+
},
18+
"devDependencies": {
19+
"nodemon": "^3.1.0",
20+
"ts-node": "^10.9.2"
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2015",
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
6+
"esModuleInterop": true,
7+
"skipLibCheck": true,
8+
"resolveJsonModule": true,
9+
"strict": true
10+
}
11+
}
12+

examples/mcp-server/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"esModuleInterop": true,
7+
"noImplicitAny": true,
8+
"resolveJsonModule": true,
9+
"strictNullChecks": true,
10+
"skipLibCheck": true,
11+
"outDir": "./dist",
12+
"rootDir": ".",
13+
}
14+
}

examples/telegram-bot/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
"dev": "jiti ./src/index.ts",
1111
"build": "tsc",
1212
"start": "node ./dist/index.js",
13-
"clean": "rm -rf dist",
14-
"lint": "eslint --fix --quiet src/**/*.ts",
15-
"format": "prettier --loglevel silent --write src/**/*.ts"
13+
"clean": "rm -rf dist"
1614
},
1715
"dependencies": {
1816
"dotenv": "^16.5.0",

0 commit comments

Comments
 (0)