Skip to content

feat(mcp): prevent agent-generated unknown session retries #683

Description

@Sechi42

?? Pre-flight Checks

  • I have searched existing issues and this is not a duplicate
  • I understand this issue needs status:approved before a PR can be opened

?? Problem Description

Agent callers frequently pass a human-generated task name as session_id to mem_session_summary without first registering it through mem_session_start. Engram correctly returns unknown_session, but the summary call then has to be repeated without the field. This consumes an additional model turn and tool call, and the same failure pattern occurs in delegated agents because the lifecycle relationship is not machine-enforced by the MCP contract.

The current error hint is useful after failure, but prevention is cheaper than recovery. More conversational context does not help because session registration is deterministic server state.

?? Proposed Solution

Make the MCP session lifecycle agent-safe while preserving strict rejection of typos:

  1. Strengthen the session_id schema description for mem_session_summary so callers are explicitly told to pass it only after a successful mem_session_start; otherwise omit it.
  2. Return structured recovery metadata on unknown_session, for example an operation identifier and safe retry arguments with session_id omitted, instead of only prose.
  3. Consider returning an opaque registered session handle from mem_session_start and accepting that handle in lifecycle tools so agents do not infer IDs from task names.
  4. Add contract tests covering primary and delegated-agent callers that attempt to summarize with an unregistered ID.

Do not silently create a session from an unknown ID by default; that could hide typos and fragment session history.

?? Affected Area

MCP Server (tools, transport)

?? Alternatives Considered

  • Rely only on prompt instructions. This remains probabilistic and has already failed in primary and delegated agents.
  • Automatically create every unknown session. This saves a retry but risks silent session fragmentation.
  • Always omit session_id. This prevents explicit multi-session attribution.

?? Additional Context

Observed failure:

{"error_code":"unknown_session","hint":"Start the session first, omit session_id, or retry with an existing session_id."}

The immediate retry without session_id succeeded and no data was lost. The issue is avoidable token and tool-call overhead plus repeated lifecycle misuse across agents.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions