index.tsā¢5.09 kB
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
// Import configuration and validation
import { validateEnvironment, isEnvironmentConfigured } from "./config.js";
// Account tools
import { CREATE_ACCOUNT, createAccountHandler } from "./tools/account/createAccount.js";
import { GET_ACCOUNT_INFO, getAccountInfoHandler } from "./tools/account/getAccountInfo.js";
import { FUND_ACCOUNT, fundAccountHandler } from "./tools/account/fundAccount.js";
// Native APT tools
import { GET_APT_BALANCE, getAptBalanceHandler } from "./tools/native/getBalance.js";
import { TRANSFER_APT, transferAptHandler } from "./tools/native/transferApt.js";
// Coin tools
import { GET_COIN_BALANCE, getCoinBalanceHandler } from "./tools/coin/getCoinBalance.js";
import { TRANSFER_COIN, transferCoinHandler } from "./tools/coin/transferCoin.js";
import { DEPLOY_COIN, deployCoinHandler } from "./tools/coin/deployCoin.js";
import { MINT_COIN, mintCoinHandler } from "./tools/coin/mintCoin.js";
import { REGISTER_COIN, registerCoinHandler } from "./tools/coin/registerCoin.js";
// Transaction tools
import { GET_TRANSACTION_STATUS, getTransactionStatusHandler } from "./tools/transaction/getTransactionStatus.js";
import { GET_ACCOUNT_TRANSACTIONS, getAccountTransactionsHandler } from "./tools/transaction/getAccountTransactions.js";
// Note: We don't validate environment on startup anymore to allow tools/list to work
// Environment validation happens only when tools are actually called
// Pre-define tools list for fast response
const TOOLS_LIST = [
// Account tools
CREATE_ACCOUNT,
GET_ACCOUNT_INFO,
FUND_ACCOUNT,
// Native APT tools
GET_APT_BALANCE,
TRANSFER_APT,
// Coin tools
GET_COIN_BALANCE,
TRANSFER_COIN,
DEPLOY_COIN,
MINT_COIN,
REGISTER_COIN,
// Transaction tools
GET_TRANSACTION_STATUS,
GET_ACCOUNT_TRANSACTIONS,
];
const server = new Server(
{
name: "aptos/blockchain-mcp",
version: "0.1.0",
},
{
capabilities: {
tools: {},
},
}
);
// Register all available tools - return pre-defined list immediately
server.setRequestHandler(ListToolsRequestSchema, async () => {
// Return tools list immediately without any async operations
return { tools: TOOLS_LIST };
});
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
// Quick validation without any async operations
if (!isEnvironmentConfigured()) {
return {
content: [
{
type: "text",
text: "Configuration required: Please set APTOS_MCP_PRIVATE_KEY environment variable with your Aptos private key.",
},
],
isError: true,
};
}
// Fast validation for critical environment variables
try {
validateEnvironment();
} catch (error) {
return {
content: [
{
type: "text",
text: `Configuration error: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
}
const { name, arguments: args } = request.params;
switch (name) {
// Account tools
case "create_account":
return await createAccountHandler(args);
case "get_account_info":
return await getAccountInfoHandler(args);
case "fund_account":
return await fundAccountHandler(args);
// Native APT tools
case "get_apt_balance":
return await getAptBalanceHandler(args);
case "transfer_apt":
return await transferAptHandler(args);
// Coin tools
case "get_coin_balance":
return await getCoinBalanceHandler(args);
case "transfer_coin":
return await transferCoinHandler(args);
case "deploy_coin":
return await deployCoinHandler(args);
case "mint_coin":
return await mintCoinHandler(args);
case "register_coin":
return await registerCoinHandler(args);
// Transaction tools
case "get_transaction_status":
return await getTransactionStatusHandler(args);
case "get_account_transactions":
return await getAccountTransactionsHandler(args);
default:
return {
content: [{ type: "text", text: `Unknown tool: ${name}` }],
isError: true,
};
}
} catch (error) {
return {
content: [
{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
}
});
async function runServer() {
try {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Aptos Blockchain MCP Server running on stdio");
} catch (error) {
console.error("Fatal error running server:", error);
process.exit(1);
}
}
// Start server immediately
runServer();