-
Notifications
You must be signed in to change notification settings - Fork 158
feat(cli): add local MCP server command #1401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MathurAditya724
wants to merge
6
commits into
main
Choose a base branch
from
codex/cli-mcp-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
463da16
feat(cli): Add local MCP server command
MathurAditya724 4d105a3
fix(cli): Resolve MCP lint errors
MathurAditya724 63cf965
fix(cli): Address MCP startup edge cases
MathurAditya724 ef22986
fix(cli): Keep MCP server running
MathurAditya724 964c35c
Merge origin/main into codex/cli-mcp-auth
MathurAditya724 3b48558
fix(cli): Build bundle before E2E tests
MathurAditya724 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
|
|
||
| `sentry mcp` starts the local stdio Sentry MCP server using the active Sentry CLI session. Authenticate once with `sentry auth login`; no separate MCP login or token cache is required. | ||
|
|
||
| ## Configure an MCP client | ||
|
|
||
| Point your MCP client at the installed Sentry CLI: | ||
|
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "sentry": { | ||
| "command": "sentry", | ||
| "args": ["mcp"] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For a self-hosted instance, first authenticate the CLI against that host. You can also override the target for this MCP server invocation: | ||
|
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "sentry": { | ||
| "command": "sentry", | ||
| "args": ["mcp", "--host=sentry.example.com"] | ||
| } | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/cli/plugins/sentry-cli/skills/sentry-cli/references/mcp.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| name: sentry-cli-mcp | ||
| version: 0.47.0-dev.0 | ||
| description: Start a local Sentry MCP server | ||
| requires: | ||
| bins: ["sentry"] | ||
| auth: true | ||
| --- | ||
|
|
||
| # Mcp Commands | ||
|
|
||
| Start a local Sentry MCP server | ||
|
|
||
| ### `sentry mcp` | ||
|
|
||
| Start a local Sentry MCP server | ||
|
|
||
| All commands also support `--json`, `--fields`, `--help`, `--log-level`, and `--verbose` flags. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { spawn } from "node:child_process"; | ||
|
|
||
| const pnpm = process.platform === "win32" ? "pnpm.cmd" : "pnpm"; | ||
| const child = spawn(pnpm, ["run", "bundle"], { | ||
| env: { | ||
| ...process.env, | ||
| SENTRY_CLIENT_ID: process.env.SENTRY_CLIENT_ID ?? "test-client-id", | ||
| }, | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| const exitCode = await new Promise<number>((resolve, reject) => { | ||
| child.once("error", reject); | ||
| child.once("close", (code) => resolve(code ?? 1)); | ||
| }); | ||
|
|
||
| if (exitCode !== 0) { | ||
| process.exitCode = exitCode; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { SentryContext } from "../context.js"; | ||
| import { buildCommand } from "../lib/command.js"; | ||
| import { CommandOutput } from "../lib/formatters/output.js"; | ||
|
|
||
| /** Documentation route for the stdio handoff in {@link runCli}. */ | ||
| export const mcpCommand = buildCommand({ | ||
| auth: false, | ||
| docs: { | ||
| brief: "Start a local Sentry MCP server", | ||
| fullDescription: | ||
| "Start the local stdio MCP server using the current Sentry CLI session. " + | ||
| "Configure an MCP client with `sentry mcp`; authenticate first with `sentry auth login`.", | ||
| }, | ||
| output: { human: (message: string) => message }, | ||
| parameters: {}, | ||
| // biome-ignore lint/suspicious/useAwait: async generator required by buildCommand | ||
| async *func(this: SentryContext) { | ||
| yield new CommandOutput( | ||
| "The local MCP server is started by running `sentry mcp` from an MCP client configuration." | ||
| ); | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { getConfiguredSentryUrl } from "./constants.js"; | ||
| import { refreshToken } from "./db/auth.js"; | ||
| import { getEnv } from "./env.js"; | ||
| import { HostScopeError, ValidationError } from "./errors.js"; | ||
| import { getActiveTokenHost, isHostTrusted } from "./token-host.js"; | ||
|
|
||
| type McpServerConfig = { | ||
| sentryHost: string; | ||
| sentryProtocol: "http" | "https"; | ||
| }; | ||
|
|
||
| function hasSentryTargetArg(args: readonly string[]): boolean { | ||
| return args.some( | ||
| (arg) => | ||
| arg === "--host" || | ||
| arg.startsWith("--host=") || | ||
| arg === "--url" || | ||
| arg.startsWith("--url=") | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Translate the CLI's URL setting into MCP's host/protocol flags. | ||
| * | ||
| * The MCP parser intentionally rejects insecure SENTRY_URL values, while the | ||
| * CLI uses them for self-hosted defaults. Passing explicit flags preserves the | ||
| * selected CLI host without letting SENTRY_URL override --insecure-http. | ||
| */ | ||
| export function prepareMcpServerArgs( | ||
| args: readonly string[], | ||
| sentryUrl = getConfiguredSentryUrl() | ||
| ): string[] { | ||
| if (!sentryUrl || hasSentryTargetArg(args)) { | ||
| return [...args]; | ||
| } | ||
|
|
||
| // biome-ignore lint/plugin: invalid URLs are passed through to the MCP parser for its normal validation error. | ||
| try { | ||
| const url = new URL(sentryUrl); | ||
| if (url.protocol !== "http:" && url.protocol !== "https:") { | ||
| return [...args, `--url=${sentryUrl}`]; | ||
| } | ||
| return [ | ||
| ...args, | ||
| `--host=${url.host}`, | ||
| ...(url.protocol === "http:" ? ["--insecure-http"] : []), | ||
| ]; | ||
| } catch { | ||
| return [...args, `--url=${sentryUrl}`]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a credential for the local MCP server from the CLI's authenticated | ||
| * session, preserving the CLI's host-scoping protections. | ||
| */ | ||
| export async function resolveCliMcpAccessToken( | ||
| config: McpServerConfig | ||
| ): Promise<string> { | ||
| const targetUrl = `${config.sentryProtocol}://${config.sentryHost}`; | ||
| const { token } = await refreshToken(); | ||
| const tokenHost = getActiveTokenHost(); | ||
|
|
||
| if (!(tokenHost && isHostTrusted(targetUrl, tokenHost))) { | ||
| throw new HostScopeError( | ||
| "Cannot start MCP server with the active CLI credentials", | ||
| targetUrl, | ||
| tokenHost | ||
| ); | ||
| } | ||
|
|
||
| return token; | ||
| } | ||
|
|
||
| /** Start the local stdio server without introducing a second auth flow. */ | ||
| export async function startMcpServer(args: string[]): Promise<void> { | ||
| if (args[0] === "auth") { | ||
| throw new ValidationError( | ||
| "Use `sentry auth` to manage credentials for `sentry mcp`." | ||
| ); | ||
| } | ||
|
|
||
| const { runMcpServer } = await import("@sentry/mcp-server"); | ||
| const { | ||
| SENTRY_HOST: _sentryHost, | ||
| SENTRY_URL: _sentryUrl, | ||
| ...mcpEnv | ||
| } = getEnv(); | ||
| await runMcpServer(prepareMcpServerArgs(args), { | ||
| environment: mcpEnv, | ||
| packageName: "sentry mcp", | ||
| resolveAccessToken: resolveCliMcpAccessToken, | ||
| throwOnError: true, | ||
| }); | ||
|
sentry-warden[bot] marked this conversation as resolved.
MathurAditya724 marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.