Python: Fix azurefunctions MCP tool invocation to use correct agent (#3339)

* MCP tool fix for azurefunctions

* Moving logic to check for thread id
This commit is contained in:
Gavin Aguiar
2026-01-22 22:36:44 +00:00
committed by GitHub
parent 87c9d74bd7
commit b072df32c5
4 changed files with 110 additions and 10 deletions
@@ -609,7 +609,7 @@ class AgentFunctionApp(DFAppBase):
# Create or parse session ID
if thread_id and isinstance(thread_id, str) and thread_id.strip():
try:
session_id = AgentSessionId.parse(thread_id)
session_id = AgentSessionId.parse(thread_id, agent_name=agent_name)
except ValueError as e:
logger.warning(
"Failed to parse AgentSessionId from thread_id '%s': %s. Falling back to new session ID.",
@@ -109,26 +109,34 @@ class AgentSessionId:
return f"AgentSessionId(name='{self.name}', key='{self.key}')"
@staticmethod
def parse(session_id_string: str) -> AgentSessionId:
def parse(session_id_string: str, agent_name: str | None = None) -> AgentSessionId:
"""Parses a string representation of an agent session ID.
Args:
session_id_string: A string in the form @name@key
session_id_string: A string in the form @name@key, or a plain key string
when agent_name is provided.
agent_name: Optional agent name to use instead of parsing from the string.
If provided, only the key portion is extracted from session_id_string
(for @name@key format) or the entire string is used as the key
(for plain strings).
Returns:
AgentSessionId instance
Raises:
ValueError: If the string format is invalid
ValueError: If the string format is invalid and agent_name is not provided
"""
if not session_id_string.startswith("@"):
raise ValueError(f"Invalid agent session ID format: {session_id_string}")
# Check if string is in @name@key format
if session_id_string.startswith("@") and "@" in session_id_string[1:]:
parts = session_id_string[1:].split("@", 1)
name = agent_name if agent_name is not None else parts[0]
return AgentSessionId(name=name, key=parts[1])
parts = session_id_string[1:].split("@", 1)
if len(parts) != 2:
raise ValueError(f"Invalid agent session ID format: {session_id_string}")
# Plain string format - only valid when agent_name is provided
if agent_name is not None:
return AgentSessionId(name=agent_name, key=session_id_string)
return AgentSessionId(name=parts[0], key=parts[1])
raise ValueError(f"Invalid agent session ID format: {session_id_string}")
class DurableAgentThread(AgentThread):