index.ts•3.29 kB
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { getPositions, tradeStock, getMarketStatus, getQuote, getOrderHistory, cancelOrder, getAccountBalance } from "./trade";
import * as fs from 'fs';
import path from 'path';
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'mcp-conf.json'), 'utf-8'));
const server = new McpServer({
name: "trade-mcp",
version: "1.0.1"
});
server.tool(
"buy-stock",
config.buyStock.prompt,
{ tradingsymbol: z.string(), quantity: z.number() },
async ({ tradingsymbol, quantity }) => {
let res = await tradeStock(tradingsymbol, quantity, "BUY");
if (res.status !== "success") {
return { content: [{ type: "text", text: `Error: ${res.message}` }] };
}
return { content: [{ type: "text", text: `Order placed for ${quantity} shares of ${tradingsymbol} to BUY and order id is ${res.order_id}`}] };
}
);
server.tool(
"sell-stock",
config.sellStock.prompt,
{ tradingsymbol: z.string(), quantity: z.number() },
async ({ tradingsymbol, quantity }) => {
let res = await tradeStock(tradingsymbol, quantity, "SELL");
if (res.status !== "success") {
return { content: [{ type: "text", text: `Error: ${res.message}` }] };
}
return { content: [{ type: "text", text: `Order placed for ${quantity} shares of ${tradingsymbol} to SELL and order id is ${res.order_id}` }] };
}
);
server.tool(
"get-positions",
config.getPosition.prompt,
{},
async () => {
const positions = await getPositions();
return { content: [{ type: "text", text: `Positions: ${JSON.stringify(positions)}` }] };
}
);
server.tool(
"get-market-status",
config.getMarketStatus.prompt,
{},
async () => {
const status = await getMarketStatus();
return { content: [{ type: "text", text: `Market Status: ${JSON.stringify(status)}` }] };
}
);
server.tool(
"get-quote",
config.getQuote.prompt,
{ tradingsymbol: z.string() },
async ({ tradingsymbol }) => {
const quote = await getQuote(tradingsymbol);
return { content: [{ type: "text", text: `Quote for ${tradingsymbol}: ${JSON.stringify(quote)}` }] };
}
);
server.tool(
"get-order-history",
config.getOrderHistory.prompt,
{},
async () => {
const orders = await getOrderHistory();
return { content: [{ type: "text", text: `Order History: ${JSON.stringify(orders)}` }] };
}
);
server.tool(
"cancel-order",
config.cancelOrder.prompt,
{ orderId: z.string() },
async ({ orderId }) => {
const result = await cancelOrder(orderId);
return { content: [{ type: "text", text: `Order Cancellation Result: ${JSON.stringify(result)}` }] };
}
);
server.tool(
"get-account-balance",
config.getAccountBalance.prompt,
{},
async () => {
const balance = await getAccountBalance();
return { content: [{ type: "text", text: `Account Balance: ${JSON.stringify(balance)}` }] };
}
);
const transport = new StdioServerTransport();
await server.connect(transport);