Make session IDs explicit tool parameters instead of auto-injecting them

Previously, Tool.apply_ex derived a session ID from the MCP session object (or
"global" with no context) and injected it into any apply() method that declared
a session_id parameter. This is being changed because:
* the new MCP SDK v2 no longer provides session identifiers
* handling it internally is more robust anyway, since clients did not
  consistently use sessions

Session handling
- Remove implicit session ID injection from Tool.apply_ex, along with the
  supporting _is_session_aware property and SESSION_ID_PARAM_NAME skip logic
- SerenaAgent.create_system_prompt() now creates the session itself and reports
  its id, instead of receiving session_id as an argument

Tool signatures
- InitialInstructionsTool.apply() and ActivateProjectTool.apply() now declare
  session_id explicitly and rely on the LLM to pass it, rather than having it
  injected
- Rename SerenaReplTool.apply()'s session parameter to session_id for consistency
  with the other tools

Resolves #2061
This commit is contained in:
Dominik Jain committed 2026-09-18 16:33:31 +02:00
1 parent 2a6deef838
commit 7a16b3db5a
8 files changed
+37 -50

No files matched your search

+10 -5
View File
@@ -17,6 +17,7 @@ from serena.config.context_mode import SerenaAgentContext
from serena.config.serena_config import AgentInterface, ProjectConfig, RegisteredProject, SerenaConfig
from serena.lsp.lsp_diagnostics import DiagnosticsContext
from serena.project import Project
from serena.session import SessionRegistry
from serena.tools import (
SUCCESS_RESULT,
ActivateProjectTool,
@@ -1400,13 +1401,17 @@ class TestSerenaAgent:
class TestPromptProvision:
class MockContext:
def __init__(self, session_id: str):
self.session = session_id
@classmethod
def _call_tool(cls, agent: SerenaAgent, tool_class: type[Tool], session_id: str = "global", **kwargs) -> str:
result = agent.get_tool(tool_class).apply_ex(mcp_ctx=cls.MockContext(session_id), catch_exceptions=False, **kwargs)
old_method = SessionRegistry._next_session_id
if tool_class == InitialInstructionsTool:
SessionRegistry._next_session_id = lambda x: session_id # type: ignore
else:
kwargs["session_id"] = session_id
try:
result = agent.get_tool(tool_class).apply_ex(catch_exceptions=False, **kwargs)
finally:
SessionRegistry._next_session_id = old_method
return result
@staticmethod