index.ts•3.25 kB
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { SentryClient } from "./sentry-client.js";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
// Get the directory of the current file
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load .env environment variables
dotenv.config({ path: path.resolve(__dirname, "../.env") });
// Get configuration from environment variables
const authToken = process.env.SENTRY_AUTH_TOKEN;
const projectSlug = process.env.SENTRY_PROJECT_SLUG;
const organizationSlug = process.env.SENTRY_ORGANIZATION_SLUG;
if (!authToken) {
console.error("Error: Missing required environment variable SENTRY_AUTH_TOKEN");
process.exit(1);
}
// Create Sentry client
const sentryClient = new SentryClient(authToken, organizationSlug, projectSlug);
// Create MCP server
const server = new McpServer({
name: "Sentry",
version: "1.0.0",
description: "MCP Server for accessing and analyzing Sentry issues"
});
// Add tool for getting Sentry issues
server.tool(
"get_sentry_issue",
{ issue_id_or_url: z.string().describe("Sentry issue ID or URL to analyze") },
async ({ issue_id_or_url }: { issue_id_or_url: string }) => {
try {
const issue = await sentryClient.getIssue(issue_id_or_url);
return {
content: [
{
type: "text",
text: JSON.stringify(issue, null, 2)
}
]
};
} catch (error) {
const errorMessage = error instanceof Error
? error.message
: "Unknown error when fetching Sentry issue";
return {
content: [{ type: "text", text: errorMessage }],
isError: true
};
}
}
);
// Add Sentry issue prompt template
server.prompt(
"sentry-issue",
{ issue_id_or_url: z.string().describe("Sentry issue ID or URL") },
async ({ issue_id_or_url }: { issue_id_or_url: string }) => {
try {
const issue = await sentryClient.getIssue(issue_id_or_url);
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `I need help analyzing this Sentry issue:
Title: ${issue.title}
Issue ID: ${issue.id}
Status: ${issue.status}
Level: ${issue.level}
First seen: ${issue.firstSeen}
Last seen: ${issue.lastSeen}
Event count: ${issue.count}
Stacktrace:
${issue.stacktrace || "No stacktrace available"}
Please help me understand this error and suggest potential fixes.`
}
}
]
};
} catch (error) {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `I tried to analyze a Sentry issue, but encountered an error:
${error instanceof Error ? error.message : "Unknown error"}`
}
}
]
};
}
}
);
// Start server
async function start() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
start();