index.ts•2.21 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import express, { Request, Response } from "express";
import { z } from "zod";
import { getPullRequestDetails, getPullRequestDiff } from "./get-diff.js";
import { createThreadComment } from "./comment.js";
// Initialize the MCP server
const server = new McpServer({
name: "AzureRevisorServer",
version: "1.0.0",
});
server.tool(
"Get pull request diff",
{
pullRequestUrl: z.string().describe("The URL of the pull request"),
},
async ({ pullRequestUrl }: { pullRequestUrl: string }) => {
const diff = await getPullRequestDiff(pullRequestUrl);
return {
content: [{ type: "text", text: diff }],
};
}
);
server.tool(
"Create thread comment",
{
pullRequestUrl: z.string().describe("The URL of the pull request"),
filePath: z.string().describe("The path of the file to comment"),
lineNumber: z.number().describe("The line number to comment"),
commentContent: z.string().describe("The content of the comment"),
},
async ({
pullRequestUrl,
filePath,
lineNumber,
commentContent,
}: {
pullRequestUrl: string;
filePath: string;
lineNumber: number;
commentContent: string;
}) => {
const prInfo = getPullRequestDetails(pullRequestUrl);
await createThreadComment(prInfo, filePath, lineNumber, commentContent);
return {
content: [{ type: "text", text: "Comment created successfully" }],
};
}
);
// Function to start the server
async function startServer() {
const app = express();
let transport: SSEServerTransport | null = null;
app.get("/sse", async (req: Request, res: Response) => {
transport = new SSEServerTransport("/messages", res);
await server.connect(transport);
});
app.post("/messages", async (req: Request, res: Response) => {
if (transport) {
await transport.handlePostMessage(req, res);
}
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
}
// Start the server
startServer();