最新资讯
dev.to 我们在 DolphinBench 上测试记忆检索:纯关键词搜索暴露了自身回归 dev.to 别再把 Agent 写成 while 循环,把它建模为状态机 bloomberg.com Rightmove 与 Scout24 借 AI 产品应对低迷房市并上调展望 stackoverflow.blog 欢迎来到芯片时代:半导体制造、AI 数据中心与芯片设计的未来 www.youtube.com AI Agent 总犯同样的错:CentralAgent AI 将记忆重构为经验系统 pub.towardsai.net Netflix 科学家建议重采样 LLM 评判:5 次仅将误差棒缩小 13% pub.towardsai.net Jev 作为 LLM 前的静默层:构建提示感知的模型路由器 reddit.com OpenAI 称 Astra 仅满足 10% 安全任务标准 dev.to FreeToken:在自有 GPU 上运行前沿 MoE 模型及其内存需求 dev.to 当 AI Agent 说“完成”时,究竟完成了什么? dev.to 部署 Underdog Bark 3.0 Pro:逐字转写、自定义后端与分块设置 dev.to 为艰难对话构建 AI:五个 Agent、一条安全正则与一位盲评监督者
多元动态
登录 注册
AI编码 dev.to · @aicoding-guide

Claude Code 中远程 MCP 服务器登录指南:/mcp 与 claude mcp login

原文 我的收藏

文章介绍在 Claude Code 中登录远程 MCP 服务器的方法:托管服务(如 Sentry、Linear、Notion)注册 URL 后需先完成浏览器登录,否则 claude mcp list 会显示 Needs authentication。可用 claude mcp add --transport http 注册,再在会话中运行 /mcp 选择 Authenticate,或在 shell 中执行 claude mcp login;文中还涉及 --callback-port 固定重定向 URI、传入预注册 client ID,以及 token 过期或浏览器未打开时的处理。

AI推荐理由 面向开发者的实操教程,说明 Claude Code 中远程 MCP 服务器的两种登录方式(/mcp 与 claude mcp login)及 token 过期、回调端口等处理,对使用托管 MCP 服务有直接帮助,但属工具用法说明,非新信息。

60

正文

Originally published at https://aicoding-guide.com. Hosted MCP servers like Sentry, Linear and Notion are not usable the moment you register their URL. They need a browser sign-in first, and claude mcp list shows them as ! Needs authentication until you do it. In short: add the server with claude mcp add --transport http , then either run /mcp in a session and choose Authenticate on that server, or run claude mcp login from your shell. Tokens are stored securely and refreshed automatically. This article is part of a series. For every way to register a server, see Adding MCP servers to Claude Code. Key point What you will learn The two ways to sign in: /mcp and claude mcp login --callback-port for a fixed redirect URI, and how to pass a pre-registered client ID What to do when a token expires or the browser never opens Add the server and sign in from /mcp Register the server over the HTTP transport. The docs use Sentry as the example. claude mcp add --transport http sentry https://mcp.sentry.dev/mcp Enter fullscreen mode Exit fullscreen mode Right after adding, claude mcp list shows ! Needs authentication. That is expected; the sign-in clears it. Start a session, run /mcp, select the server from the list, press Enter and choose Authenticate. Your browser opens the service's sign-in page, where you approve the connection. Back in Claude Code the server's status changes to connected. Claude Code marks a remote server as needing authentication when it answers with 401 Unauthorized or 403 Forbidden. There is also a startup notice listing servers that need sign-in, so you do not have to open /mcp to find them (v2.1.193 or later). Glossary Dynamic Client Registration: an OAuth mechanism where the client registers itself with the authorization server instead of you registering an app by hand. When a server supports it, passing the URL to claude mcp add is all the setup the sign-in needs. Sign in from the command line To sign in without opening a session, use claude mcp login. claude mcp login sentry Enter fullscreen mode Exit fullscreen mode To clear stored credentials later, run claude mcp logout . Over SSH, or on Linux without a display server, the command detects that no local browser is available and prints the authorization URL rather than trying to open one. Open that URL on your own machine, then paste the full redirect URL from the address bar back at the prompt. The paste step needs an interactive terminal, so connect with ssh -t. Pass --no-browser to force the URL prompt even when a browser is detected. claude mcp login sentry --no-browser Enter fullscreen mode Exit fullscreen mode Non-interactive runs (claude -p, the Agent SDK) have no /mcp panel, so Claude Code cannot run the OAuth flow there. As of v2.1.196, with tool search enabled, it tells Claude that the server's tools are unavailable until you authorize it, so Claude can name the server instead of acting as though it were not configured. Do the sign-in itself from an interactive session or with claude mcp login. Use pre-registered OAuth credentials If a server does not support Dynamic Client Registration, you get an error such as "Incompatible auth server: does not support dynamic client registration". Register an OAuth app through the service's developer portal and pass the credentials yourself. Servers that require a registered redirect URI expect the form http://localhost:PORT/callback. Pick a port, register it, and pass the same port to --callback-port: by default Claude Code picks a random free port, which will never match. Flag / key What it does --callback-port Fixes the callback port. Usable on its own --client-id The client ID of your registered OAuth app --client-secret Prompts for the secret with masked input MCP_CLIENT_SECRET Supplies the secret through the environment, skipping the prompt claude mcp add --transport http \ --client-id your-client-id --client-secret --callback-port 8080 \ my-server https://mcp.example.com/mcp Enter fullscreen mode Exit fullscreen mode In JSON, the same settings live in an oauth object. The secret stays out of the JSON and is passed with the separate --client-secret flag. claude mcp add-json my-server \ '{"type":"http","url":"https://mcp.example.com/mcp","oauth":{"clientId":"your-client-id","callbackPort":8080}}' \ --client-secret Enter fullscreen mode Exit fullscreen mode oauth also takes scopes, which pins the scopes requested during authorization (a single space-separated string, matching RFC 6749), and authServerMetadataUrl, which overrides metadata discovery. { "mcpServers": { "slack": { "type": "http", "url": "https://mcp.slack.com/mcp", "oauth": { "scopes": "channels:read chat:write search:read" } } } } Enter fullscreen mode Exit fullscreen mode oauth.scopes takes precedence over authServerMetadataUrl and over whatever the server advertises at /.well-known. If the authorization server advertises offline_access, Claude Code appends it so the token can be refreshed without another browser sign-in. The secret can only be set when you add the server The client secret is stored in your system keychain (macOS) or a credentials file, never in your config. You can set it only at add time: when you authenticate with claude mcp login or from /mcp, Claude Code uses the stored secret and neither prompts for one nor reads MCP_CLIENT_SECRET. To change it, run claude mcp remove and add the server again with --client-secret and the same --scope. Use claude mcp get to check whether credentials are configured. Which file each scope writes to is covered in claude mcp add scopes: local, project and user, and passing a static token through a header in Passing tokens from environment variables in .mcp.json. When sign-in expires or fails When a request to a server you already signed in to returns 401, Claude Code refreshes the stored token, reconnects and retries the request once. It flags the server in /mcp only if that retry also fails. When the server rejects the stored refresh token, a notice pointing at /mcp appears immediately. Open /mcp and choose Re-authenticate on that server. Symptom What to do The browser does not open Copy the URL shown in the terminal and open it manually The redirect fails with a connection error after you authenticate Paste the full callback URL from the address bar into the URL prompt Claude Code shows A server with a configured Authorization header returns 401 or 403 It will not fall back to OAuth; the connection is reported as failed. Check the token, or remove the header A tool call fails with 403 insufficient_scope Add the scope the server names to oauth.scopes, then authenticate again from /mcp That last row catches people out: Claude Code requests the scopes you pinned, not the scope the server asked for, so signing in again without adding it gives you a token that still lacks it. One gap worth naming: the on-disk location of OAuth access and refresh tokens could not be confirmed in the official documentation. It says only that tokens are "stored securely and refreshed automatically", and names the keychain or a credentials file for the client secret alone. Summary Add with claude mcp add --transport http , then sign in from /mcp → Authenticate or with claude mcp login Revoke with claude mcp logout or "Clear authentication" in the /mcp menu On a headless box, claude mcp login --no-browser prints the URL and takes the redirect URL back Without Dynamic Client Registration, use --client-id and --callback-port, plus --client-secret if the app has one A rejected refresh token means Re-authenticate in /mcp; a missing scope means editing oauth.scopes first

来源报道 · 1 篇 · 以下链接均前往原始媒体

← 返回精选动态 原文

更多内容