mcp: no reconnect when a legacy HTTP server answers an expired session with 400 "No valid session ID provided"

Aberta Para iniciantes
#66,073 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
72/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
typescript
Domínio
api

Direção de pesquisa

Comece em packages/core/src/mcp/client.ts em failure() (por volta das linhas 497-512), que mapeia um 404 e um 400 Bad Request: Server not initialized para SessionExpiredError; o novo caso é um 400 cujo body contém No valid session ID, ainda condicionado ao transporte possuir um session id. Em seguida, leia connection.ts:139-149 para confirmar que recovering é o que dispara a reconexão, para que você possa saber que o fix funciona. Adicione ou estenda um bun test em packages/core/test que responda a um session id antigo com o body 400 do relatório e afirme que a chamada tenta novamente e tem sucesso. Observe que quem reportou se ofereceu para enviar um PR com um teste, então verifique a thread antes de começar.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

area/mcp platform/windows status/repro-simulated type/bug version/v2
Description

When a legacy Streamable HTTP server forgets the session (restart or idle expiry) and answers the old Mcp-Session-Id with HTTP 400 Bad Request: No valid session ID provided, opencode does not reconnect. Every later tool call on that server fails with that 400, and the server still shows connected until the user reconnects it by hand.

This was reported in #32809 (Atlassian MCP 1.12.4 after ~15-30 min idle). The bot closed it for inactivity and asked for a new issue if it is still relevant. It still happens on v2 dev.

The cause is failure() in packages/core/src/mcp/client.ts:497-512. It maps a 404 to SessionExpiredError, but a 400 only when the body contains Bad Request: Server not initialized. Older Python SDK session managers and the TypeScript SDK example servers answer an unknown session id with Bad Request: No valid session ID provided. FastMCP-based servers such as mcp-atlassian inherit this. That body falls through as a plain error, so recovering (connection.ts:139-149) never runs.

Fix idea: also treat a 400 whose body contains No valid session ID as an expired session. The check stays narrow: it only applies when the transport holds a session id, like the existing cases, so other 400s still pass through.

I can send a PR with a test if this is OK.

Plugins

No response

OpenCode version

dev adc958a7d7 (2.4.1)

Steps to reproduce
  1. Run a stateful legacy MCP server that answers an unknown session id with 400 {"jsonrpc":"2.0","error":{"code":-32000,"message":"Bad Request: No valid session ID provided"},"id":null}, and add it as a remote server.
  2. Call a tool once. It works.
  3. Restart the server (or let the session expire).
  4. Call the tool again. It fails with MCP tool "echo" on server "legacy" failed: Error POSTing to endpoint: {..."Bad Request: No valid session ID provided"...} (HTTP 400), and every later call fails the same way. With a 404 for the same case, opencode reconnects and the call succeeds.

I have this as a bun test in packages/core/test.

Screenshot

No response

Operating System

Windows 11

Terminal

No response

Linguagem predominante
TypeScript
Estrelas
228k
Forks
31k
Merge médio
3h 22min
PRs com merge (30d)
476

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de anomalyco/opencode

Todas as issues de anomalyco/opencode

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.