Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: patch mcp attach bug (#3844)

Charles Packer68974ff8821f
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index 6cae38aef..cc6992159 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -410,32 +410,37 @@ class ToolManager:
new_name = None
new_schema = None
- # TODO: Consider this behavior...is this what we want?
- # TODO: I feel like it's bad if json_schema strays from source code so
- # if source code is provided, always derive the name from it
- if "source_code" in update_data.keys() and not bypass_name_check:
- # derive the schema from source code to get the function name
- derived_schema = derive_openai_json_schema(source_code=update_data["source_code"])
- new_name = derived_schema["name"]
-
- # if json_schema wasn't provided, use the derived schema
- if "json_schema" not in update_data.keys():
- new_schema = derived_schema
- else:
- # if json_schema was provided, update only its name to match the source code
- new_schema = update_data["json_schema"].copy()
- new_schema["name"] = new_name
- # update the json_schema in update_data so it gets applied in the loop
- update_data["json_schema"] = new_schema
+ # Fetch current tool to allow conditional logic based on tool type
+ current_tool = self.get_tool_by_id(tool_id=tool_id, actor=actor)
- # get current tool to check if name is changing
- current_tool = self.get_tool_by_id(tool_id=tool_id, actor=actor)
- # check if the name is changing and if so, verify it doesn't conflict
+ # For MCP tools, do NOT derive schema from Python source. Trust provided JSON schema.
+ if current_tool.tool_type == ToolType.EXTERNAL_MCP:
+ if "json_schema" in update_data:
+ new_schema = update_data["json_schema"].copy()
+ new_name = new_schema.get("name", current_tool.name)
+ else:
+ new_schema = current_tool.json_schema
+ new_name = current_tool.name
+ update_data.pop("source_code", None)
if new_name != current_tool.name:
- # check if a tool with the new name already exists
existing_tool = self.get_tool_by_name(tool_name=new_name, actor=actor)
if existing_tool:
raise LettaToolNameConflictError(tool_name=new_name)
+ else:
+ # For non-MCP tools, preserve existing behavior
+ if "source_code" in update_data.keys() and not bypass_name_check:
+ derived_schema = derive_openai_json_schema(source_code=update_data["source_code"])
+ new_name = derived_schema["name"]
+ if "json_schema" not in update_data.keys():
+ new_schema = derived_schema
+ else:
+ new_schema = update_data["json_schema"].copy()
+ new_schema["name"] = new_name
+ update_data["json_schema"] = new_schema
+ if new_name != current_tool.name:
+ existing_tool = self.get_tool_by_name(tool_name=new_name, actor=actor)
+ if existing_tool:
+ raise LettaToolNameConflictError(tool_name=new_name)
# Now perform the update within the session
with db_registry.session() as session:
@@ -473,32 +478,51 @@ class ToolManager:
new_name = None
new_schema = None
- # TODO: Consider this behavior...is this what we want?
- # TODO: I feel like it's bad if json_schema strays from source code so
- # if source code is provided, always derive the name from it
- if "source_code" in update_data.keys() and not bypass_name_check:
- # derive the schema from source code to get the function name
- derived_schema = derive_openai_json_schema(source_code=update_data["source_code"])
- new_name = derived_schema["name"]
-
- # if json_schema wasn't provided, use the derived schema
- if "json_schema" not in update_data.keys():
- new_schema = derived_schema
- else:
- # if json_schema was provided, update only its name to match the source code
- new_schema = update_data["json_schema"].copy()
- new_schema["name"] = new_name
- # update the json_schema in update_data so it gets applied in the loop
- update_data["json_schema"] = new_schema
+ # Fetch current tool early to allow conditional logic based on tool type
+ current_tool = await self.get_tool_by_id_async(tool_id=tool_id, actor=actor)
- # get current tool to check if name is changing
- current_tool = await self.get_tool_by_id_async(tool_id=tool_id, actor=actor)
- # check if the name is changing and if so, verify it doesn't conflict
+ # For MCP tools, do NOT derive schema from Python source. Trust provided JSON schema.
+ if current_tool.tool_type == ToolType.EXTERNAL_MCP:
+ # Prefer provided json_schema; fall back to current
+ if "json_schema" in update_data:
+ new_schema = update_data["json_schema"].copy()
+ new_name = new_schema.get("name", current_tool.name)
+ else:
+ new_schema = current_tool.json_schema
+ new_name = current_tool.name
+ # Ensure we don't trigger derive
+ update_data.pop("source_code", None)
+ # If name changes, enforce uniqueness
if new_name != current_tool.name:
- # check if a tool with the new name already exists
name_exists = await self.tool_name_exists_async(tool_name=new_name, actor=actor)
if name_exists:
raise LettaToolNameConflictError(tool_name=new_name)
+ else:
+ # For non-MCP tools, preserve existing behavior
+ # TODO: Consider this behavior...is this what we want?
+ # TODO: I feel like it's bad if json_schema strays from source code so
+ # if source code is provided, always derive the name from it
+ if "source_code" in update_data.keys() and not bypass_name_check:
+ # derive the schema from source code to get the function name
+ derived_schema = derive_openai_json_schema(source_code=update_data["source_code"])
+ new_name = derived_schema["name"]
+
+ # if json_schema wasn't provided, use the derived schema
+ if "json_schema" not in update_data.keys():
+ new_schema = derived_schema
+ else:
+ # if json_schema was provided, update only its name to match the source code
+ new_schema = update_data["json_schema"].copy()
+ new_schema["name"] = new_name
+ # update the json_schema in update_data so it gets applied in the loop
+ update_data["json_schema"] = new_schema
+
+ # check if the name is changing and if so, verify it doesn't conflict
+ if new_name != current_tool.name:
+ # check if a tool with the new name already exists
+ name_exists = await self.tool_name_exists_async(tool_name=new_name, actor=actor)
+ if name_exists:
+ raise LettaToolNameConflictError(tool_name=new_name)
# Now perform the update within the session
async with db_registry.async_session() as session:
diff --git a/tests/mcp/test_mcp.py b/tests/mcp/test_mcp.py
index 6d8edaca9..27a0b1358 100644
--- a/tests/mcp/test_mcp.py
+++ b/tests/mcp/test_mcp.py
@@ -7,10 +7,11 @@ import venv
from pathlib import Path
import pytest
+import requests
from dotenv import load_dotenv
from letta_client import Letta, McpTool, ToolCallMessage, ToolReturnMessage
-from letta.functions.mcp_client.types import SSEServerConfig, StdioServerConfig
+from letta.functions.mcp_client.types import SSEServerConfig, StdioServerConfig, StreamableHTTPServerConfig
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.letta_message_content import TextContent
from letta.schemas.llm_config import LLMConfig
@@ -207,3 +208,44 @@ def test_stdio_mcp_server(client, agent_state):
assert ret.status == "success", f"Unexpected status: {ret.status}"
# make sure there's at least some payload
assert len(ret.tool_return.strip()) >= 10, f"Expected at least 10 characters in tool_return, got {len(ret.tool_return.strip())}"
+
+
+async def test_streamable_http_mcp_server_update_schema_no_docstring_required(client, agent_state, server_url):
+ """
+ Repro for schema-derivation-on-update error with MCP tools.
+
+ Without the fix, calling add_mcp_tool a second time for the same MCP tool
+ triggers a docstring-based schema derivation on a generated wrapper that has
+ no docstring, causing a 500. With the fix in place, updates should succeed.
+ """
+ mcp_server_name = f"deepwiki_http_{uuid.uuid4().hex[:6]}"
+ mcp_url = "https://mcp.deepwiki.com/mcp"
+
+ http_mcp_config = StreamableHTTPServerConfig(server_name=mcp_server_name, server_url=mcp_url)
+ try:
+ client.tools.add_mcp_server(request=http_mcp_config)
+
+ # Ensure server is registered
+ servers = client.tools.list_mcp_servers()
+ assert mcp_server_name in servers
+
+ # Fetch available tools from server
+ tools = client.tools.list_mcp_tools_by_server(mcp_server_name=mcp_server_name)
+ assert tools, "Expected at least one tool from deepwiki streamable-http MCP server"
+ ask_question_tool = next((t for t in tools if t.name == "ask_question"), None)
+ assert ask_question_tool is not None, f"ask_question tool not found. Available: {[t.name for t in tools]}"
+
+ # Initial create
+ letta_tool_1 = client.tools.add_mcp_tool(mcp_server_name=mcp_server_name, mcp_tool_name=ask_question_tool.name)
+ assert letta_tool_1 is not None
+
+ # Update path (re-register same tool); should not attempt Python docstring schema derivation
+ letta_tool_2 = client.tools.add_mcp_tool(mcp_server_name=mcp_server_name, mcp_tool_name=ask_question_tool.name)
+ assert letta_tool_2 is not None
+ finally:
+ # Best-effort cleanup (DELETE by name)
+ try:
+ requests.delete(f"{server_url}/v1/tools/mcp/servers/{mcp_server_name}")
+ except Exception:
+ pass