Skip to content

Your team of AI agents, on your Mac, Windows or Linux. Agents that talk in channels, write code, review each other and use your tools, on the Claude Code, Codex, OpenCode or Pi you already have. Anything that touches the real world waits for your yes.

Say a connector's token was refused whatever the 401 says (#99)

Eduardo Machadoc88879db2369
diff --git a/src/core/connectors.ts b/src/core/connectors.ts
index 9ebfa79..6bbfd16 100644
--- a/src/core/connectors.ts
+++ b/src/core/connectors.ts
@@ -5,7 +5,7 @@ import { randomBytes, randomUUID } from 'node:crypto'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { UnauthorizedError, type OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'
import { getDefaultEnvironment, StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
-import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
+import { StreamableHTTPClientTransport, StreamableHTTPError } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
import type { OAuthClientInformationMixed, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js'
@@ -200,7 +200,10 @@ export class ConnectorManager {
try {
await client.connect(transport, { timeout: CONNECT_TIMEOUT_MS })
} catch (e) {
- const refused = e instanceof UnauthorizedError || (e instanceof Error && /unauthorized|401/i.test(e.message))
+ // A 401 without a sign-in flow comes back as the server's own body ("invalid_token" on
+ // Linear's), so its status says it, not its text.
+ const refused =
+ e instanceof UnauthorizedError || (e instanceof StreamableHTTPError && e.code === 401) || (e instanceof Error && /unauthorized|401/i.test(e.message))
if (refused && c.auth === 'token') {
// With a token there is no browser to go to: the server refused it, or there is none yet.
await client.close().catch(() => {})
diff --git a/test/connectors.test.ts b/test/connectors.test.ts
index bd8f8ba..e627f25 100644
--- a/test/connectors.test.ts
+++ b/test/connectors.test.ts
@@ -124,6 +124,21 @@ describe('a connector with a token', () => {
srv.close()
})
+ it("says the token was refused whatever the server writes with its 401, like Linear's", async () => {
+ const srv = createServer((_req, res) => {
+ res.writeHead(401, { 'content-type': 'application/json' }).end('{"error":"invalid_token","error_description":"Missing or invalid access token"}')
+ })
+ await new Promise<void>((resolve) => srv.listen(0, '127.0.0.1', resolve))
+ const url = `http://127.0.0.1:${(srv.address() as AddressInfo).port}/mcp`
+ const withToken = await connectors.add({ kind: 'remote', name: 'Linear', url, auth: 'token' })
+ await connectors.connect(withToken)
+ expect(connectors.get(withToken)).toMatchObject({ status: 'error', error: expect.stringMatching(/recusou o token|refused the token/) })
+ const withNone = await connectors.add({ kind: 'remote', name: 'Other', url })
+ await connectors.connect(withNone)
+ expect(connectors.get(withNone)).toMatchObject({ status: 'error', error: expect.stringMatching(/pede autorização|asks for authorization/) })
+ srv.close()
+ })
+
it('a token saved during a connection attempt is the one tried next', async () => {
const seen: (string | undefined)[] = []
const srv = createServer((req, res) => {