Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: revise mcp tool routes (#5195)
Sarah Wooders08be90f5e9ba
diff --git a/fern/openapi.json b/fern/openapi.jsonindex e0ab9604f..c99b5fc31 100644--- a/fern/openapi.json+++ b/fern/openapi.json@@ -33934,7 +33934,7 @@"server_url": {"type": "string","title": "Server Url",- "description": "The URL of the server (MCP SSE client will connect to this URL)"+ "description": "The URL of the server"},"auth_header": {"anyOf": [@@ -33973,13 +33973,13 @@}],"title": "Custom Headers",- "description": "Custom HTTP headers to include with SSE requests"+ "description": "Custom HTTP headers to include with requests"}},"type": "object","required": ["server_name", "server_url"],"title": "SSEServerConfig",- "description": "Configuration for an MCP server using SSE\n\nAuthentication can be provided in multiple ways:\n1. Using auth_header + auth_token: Will add a specific header with the token\n Example: auth_header=\"Authorization\", auth_token=\"Bearer abc123\"\n\n2. Using the custom_headers dict: For more complex authentication scenarios\n Example: custom_headers={\"X-API-Key\": \"abc123\", \"X-Custom-Header\": \"value\"}"+ "description": "Configuration for an MCP server using SSE"},"SandboxConfig": {"properties": {@@ -35265,7 +35265,7 @@"server_url": {"type": "string","title": "Server Url",- "description": "The URL path for the streamable HTTP server (e.g., 'example/mcp')"+ "description": "The URL of the server"},"auth_header": {"anyOf": [@@ -35304,13 +35304,13 @@}],"title": "Custom Headers",- "description": "Custom HTTP headers to include with streamable HTTP requests"+ "description": "Custom HTTP headers to include with requests"}},"type": "object","required": ["server_name", "server_url"],"title": "StreamableHTTPServerConfig",- "description": "Configuration for an MCP server using Streamable HTTP\n\nAuthentication can be provided in multiple ways:\n1. Using auth_header + auth_token: Will add a specific header with the token\n Example: auth_header=\"Authorization\", auth_token=\"Bearer abc123\"\n\n2. Using the custom_headers dict: For more complex authentication scenarios\n Example: custom_headers={\"X-API-Key\": \"abc123\", \"X-Custom-Header\": \"value\"}"+ "description": "Configuration for an MCP server using Streamable HTTP"},"SummarizedReasoningContent": {"properties": {diff --git a/letta/functions/mcp_client/types.py b/letta/functions/mcp_client/types.pyindex b1cd27e21..74293d81a 100644--- a/letta/functions/mcp_client/types.py+++ b/letta/functions/mcp_client/types.py@@ -129,9 +129,9 @@ class BaseServerConfig(BaseModel):raise NotImplementedError-class SSEServerConfig(BaseServerConfig):+class HTTPBasedServerConfig(BaseServerConfig):"""- Configuration for an MCP server using SSE+ Base configuration for HTTP-based MCP servers (SSE and Streamable HTTP).Authentication can be provided in multiple ways:1. Using auth_header + auth_token: Will add a specific header with the token@@ -141,11 +141,10 @@ class SSEServerConfig(BaseServerConfig):Example: custom_headers={"X-API-Key": "abc123", "X-Custom-Header": "value"}"""- type: MCPServerType = MCPServerType.SSE- server_url: str = Field(..., description="The URL of the server (MCP SSE client will connect to this URL)")+ server_url: str = Field(..., description="The URL of the server")auth_header: Optional[str] = Field(None, description="The name of the authentication header (e.g., 'Authorization')")auth_token: Optional[str] = Field(None, description="The authentication token or API key value")- custom_headers: Optional[dict[str, str]] = Field(None, description="Custom HTTP headers to include with SSE requests")+ custom_headers: Optional[dict[str, str]] = Field(None, description="Custom HTTP headers to include with requests")def resolve_token(self) -> Optional[str]:"""@@ -170,13 +169,13 @@ class SSEServerConfig(BaseServerConfig):self.custom_headers = super().resolve_custom_headers(self.custom_headers, environment_variables)- def to_dict(self) -> dict:- values = {- "transport": "sse",- "url": self.server_url,- }+ def _build_headers_dict(self) -> Optional[dict[str, str]]:+ """+ Build headers dictionary from custom_headers and auth_header/auth_token.- # TODO: handle custom headers+ Returns:+ Dictionary of headers or None if no headers are configured+ """if self.custom_headers is not None or (self.auth_header is not None and self.auth_token is not None):headers = self.custom_headers.copy() if self.custom_headers else {}@@ -184,6 +183,24 @@ class SSEServerConfig(BaseServerConfig):if self.auth_header is not None and self.auth_token is not None:headers[self.auth_header] = self.auth_token+ return headers+ return None+++class SSEServerConfig(HTTPBasedServerConfig):+ """Configuration for an MCP server using SSE"""++ type: MCPServerType = MCPServerType.SSE++ def to_dict(self) -> dict:+ values = {+ "transport": "sse",+ "url": self.server_url,+ }++ # Handle custom headers using shared method+ headers = self._build_headers_dict()+ if headers:values["headers"] = headersreturn values@@ -210,46 +227,10 @@ class StdioServerConfig(BaseServerConfig):return values-class StreamableHTTPServerConfig(BaseServerConfig):- """- Configuration for an MCP server using Streamable HTTP-- Authentication can be provided in multiple ways:- 1. Using auth_header + auth_token: Will add a specific header with the token- Example: auth_header="Authorization", auth_token="Bearer abc123"-- 2. Using the custom_headers dict: For more complex authentication scenarios- Example: custom_headers={"X-API-Key": "abc123", "X-Custom-Header": "value"}- """+class StreamableHTTPServerConfig(HTTPBasedServerConfig):+ """Configuration for an MCP server using Streamable HTTP"""type: MCPServerType = MCPServerType.STREAMABLE_HTTP- server_url: str = Field(..., description="The URL path for the streamable HTTP server (e.g., 'example/mcp')")- auth_header: Optional[str] = Field(None, description="The name of the authentication header (e.g., 'Authorization')")- auth_token: Optional[str] = Field(None, description="The authentication token or API key value")- custom_headers: Optional[dict[str, str]] = Field(None, description="Custom HTTP headers to include with streamable HTTP requests")-- def resolve_token(self) -> Optional[str]:- """- Extract token for storage if auth_header/auth_token are provided- and not already in custom_headers.-- Returns:- The resolved token (without Bearer prefix) if it should be stored separately, None otherwise- """- if self.auth_token and self.auth_header:- # Check if custom_headers already has the auth header- if not self.custom_headers or self.auth_header not in self.custom_headers:- # Strip Bearer prefix if present- if self.auth_token.startswith(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} "):- return self.auth_token[len(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} ") :]- return self.auth_token- return None-- def resolve_environment_variables(self, environment_variables: Optional[Dict[str, str]] = None) -> None:- if self.auth_token and super().is_templated_tool_variable(self.auth_token):- self.auth_token = super().get_tool_variable(self.auth_token, environment_variables)-- self.custom_headers = super().resolve_custom_headers(self.custom_headers, environment_variables)def model_post_init(self, __context) -> None:"""Validate the server URL format."""@@ -275,14 +256,9 @@ class StreamableHTTPServerConfig(BaseServerConfig):"url": self.server_url,}- # Handle custom headers- if self.custom_headers is not None or (self.auth_header is not None and self.auth_token is not None):- headers = self.custom_headers.copy() if self.custom_headers else {}-- # Add auth header if specified- if self.auth_header is not None and self.auth_token is not None:- headers[self.auth_header] = self.auth_token-+ # Handle custom headers using shared method+ headers = self._build_headers_dict()+ if headers:values["headers"] = headersreturn valuesdiff --git a/letta/schemas/mcp_server.py b/letta/schemas/mcp_server.pynew file mode 100644index 000000000..28d47fad7--- /dev/null+++ b/letta/schemas/mcp_server.py@@ -0,0 +1,298 @@+import json+from datetime import datetime+from typing import Any, Dict, List, Optional, Union++from pydantic import Field++from letta.functions.mcp_client.types import (+ MCP_AUTH_HEADER_AUTHORIZATION,+ MCP_AUTH_TOKEN_BEARER_PREFIX,+ MCPServerType,+ SSEServerConfig,+ StdioServerConfig,+ StreamableHTTPServerConfig,+)+from letta.orm.mcp_oauth import OAuthSessionStatus+from letta.schemas.letta_base import LettaBase+from letta.schemas.secret import Secret+++class BaseMCPServer(LettaBase):+ __id_prefix__ = "mcp_server"+++# Create Schemas (for POST requests)+class CreateStdioMCPServer(StdioServerConfig):+ """Create a new Stdio MCP server"""+++class CreateSSEMCPServer(SSEServerConfig):+ """Create a new SSE MCP server"""+++class CreateStreamableHTTPMCPServer(StreamableHTTPServerConfig):+ """Create a new Streamable HTTP MCP server"""+++CreateMCPServerUnion = Union[CreateStdioMCPServer, CreateSSEMCPServer, CreateStreamableHTTPMCPServer]+++class StdioMCPServer(CreateStdioMCPServer):+ """A Stdio MCP server"""++ id: str = BaseMCPServer.generate_id_field()+++class SSEMCPServer(CreateSSEMCPServer):+ """An SSE MCP server"""++ id: str = BaseMCPServer.generate_id_field()+++class StreamableHTTPMCPServer(CreateStreamableHTTPMCPServer):+ """A Streamable HTTP MCP server"""++ id: str = BaseMCPServer.generate_id_field()+++MCPServerUnion = Union[StdioMCPServer, SSEMCPServer, StreamableHTTPMCPServer]+++# Update Schemas (for PATCH requests) - same shape as Create/Config, but all fields optional.+# We exclude fields that aren't persisted on the server model to avoid invalid ORM assignments.+class UpdateStdioMCPServer(LettaBase):+ """Update schema for Stdio MCP server - all fields optional"""++ server_name: Optional[str] = Field(None, description="The name of the MCP server")+ command: Optional[str] = Field(None, description="The command to run the MCP server")+ args: Optional[List[str]] = Field(None, description="The arguments to pass to the command")+ env: Optional[Dict[str, str]] = Field(None, description="Environment variables to set")+++class UpdateSSEMCPServer(LettaBase):+ """Update schema for SSE MCP server - all fields optional"""++ server_name: Optional[str] = Field(None, description="The name of the MCP server")+ server_url: Optional[str] = Field(None, description="The URL of the SSE MCP server")+ # Note: auth_token is renamed to token to match the ORM field+ token: Optional[str] = Field(None, description="The authentication token")+ # auth_header is excluded as it's derived from the token+ custom_headers: Optional[Dict[str, str]] = Field(None, description="Custom headers to send with requests")+++class UpdateStreamableHTTPMCPServer(LettaBase):+ """Update schema for Streamable HTTP MCP server - all fields optional"""++ server_name: Optional[str] = Field(None, description="The name of the MCP server")+ server_url: Optional[str] = Field(None, description="The URL of the Streamable HTTP MCP server")+ # Note: auth_token is renamed to token to match the ORM field+ token: Optional[str] = Field(None, description="The authentication token")+ # auth_header is excluded as it's derived from the token+ custom_headers: Optional[Dict[str, str]] = Field(None, description="Custom headers to send with requests")+++UpdateMCPServerUnion = Union[UpdateStdioMCPServer, UpdateSSEMCPServer, UpdateStreamableHTTPMCPServer]+++# OAuth-related schemas+class BaseMCPOAuth(LettaBase):+ __id_prefix__ = "mcp-oauth"+++class MCPOAuthSession(BaseMCPOAuth):+ """OAuth session for MCP server authentication."""++ id: str = BaseMCPOAuth.generate_id_field()+ state: str = Field(..., description="OAuth state parameter")+ server_id: Optional[str] = Field(None, description="MCP server ID")+ server_url: str = Field(..., description="MCP server URL")+ server_name: str = Field(..., description="MCP server display name")++ # User and organization context+ user_id: Optional[str] = Field(None, description="User ID associated with the session")+ organization_id: str = Field(..., description="Organization ID associated with the session")++ # OAuth flow data+ authorization_url: Optional[str] = Field(None, description="OAuth authorization URL")+ authorization_code: Optional[str] = Field(None, description="OAuth authorization code")++ # Encrypted authorization code (for internal use)+ authorization_code_enc: Secret | None = Field(None, description="Encrypted OAuth authorization code as Secret object")++ # Token data+ access_token: Optional[str] = Field(None, description="OAuth access token")+ refresh_token: Optional[str] = Field(None, description="OAuth refresh token")+ token_type: str = Field(default="Bearer", description="Token type")+ expires_at: Optional[datetime] = Field(None, description="Token expiry time")+ scope: Optional[str] = Field(None, description="OAuth scope")++ # Encrypted token fields (for internal use)+ access_token_enc: Secret | None = Field(None, description="Encrypted OAuth access token as Secret object")+ refresh_token_enc: Secret | None = Field(None, description="Encrypted OAuth refresh token as Secret object")++ # Client configuration+ client_id: Optional[str] = Field(None, description="OAuth client ID")+ client_secret: Optional[str] = Field(None, description="OAuth client secret")+ redirect_uri: Optional[str] = Field(None, description="OAuth redirect URI")++ # Encrypted client secret (for internal use)+ client_secret_enc: Secret | None = Field(None, description="Encrypted OAuth client secret as Secret object")++ # Session state+ status: OAuthSessionStatus = Field(default=OAuthSessionStatus.PENDING, description="Session status")++ # Timestamps+ created_at: datetime = Field(default_factory=datetime.now, description="Session creation time")+ updated_at: datetime = Field(default_factory=datetime.now, description="Last update time")++ def get_access_token_secret(self) -> Secret:+ """Get the access token as a Secret object, preferring encrypted over plaintext."""+ if self.access_token_enc is not None:+ return self.access_token_enc+ return Secret.from_db(None, self.access_token)++ def get_refresh_token_secret(self) -> Secret:+ """Get the refresh token as a Secret object, preferring encrypted over plaintext."""+ if self.refresh_token_enc is not None:+ return self.refresh_token_enc+ return Secret.from_db(None, self.refresh_token)++ def get_client_secret_secret(self) -> Secret:+ """Get the client secret as a Secret object, preferring encrypted over plaintext."""+ if self.client_secret_enc is not None:+ return self.client_secret_enc+ return Secret.from_db(None, self.client_secret)++ def get_authorization_code_secret(self) -> Secret:+ """Get the authorization code as a Secret object, preferring encrypted over plaintext."""+ if self.authorization_code_enc is not None:+ return self.authorization_code_enc+ return Secret.from_db(None, self.authorization_code)++ def set_access_token_secret(self, secret: Secret) -> None:+ """Set access token from a Secret object."""+ self.access_token_enc = secret+ secret_dict = secret.to_dict()+ if not secret.was_encrypted:+ self.access_token = secret_dict["plaintext"]+ else:+ self.access_token = None++ def set_refresh_token_secret(self, secret: Secret) -> None:+ """Set refresh token from a Secret object."""+ self.refresh_token_enc = secret+ secret_dict = secret.to_dict()+ if not secret.was_encrypted:+ self.refresh_token = secret_dict["plaintext"]+ else:+ self.refresh_token = None++ def set_client_secret_secret(self, secret: Secret) -> None:+ """Set client secret from a Secret object."""+ self.client_secret_enc = secret+ secret_dict = secret.to_dict()+ if not secret.was_encrypted:+ self.client_secret = secret_dict["plaintext"]+ else:+ self.client_secret = None++ def set_authorization_code_secret(self, secret: Secret) -> None:+ """Set authorization code from a Secret object."""+ self.authorization_code_enc = secret+ secret_dict = secret.to_dict()+ if not secret.was_encrypted:+ self.authorization_code = secret_dict["plaintext"]+ else:+ self.authorization_code = None+++class MCPOAuthSessionCreate(BaseMCPOAuth):+ """Create a new OAuth session."""++ server_url: str = Field(..., description="MCP server URL")+ server_name: str = Field(..., description="MCP server display name")+ user_id: Optional[str] = Field(None, description="User ID associated with the session")+ organization_id: str = Field(..., description="Organization ID associated with the session")+ state: Optional[str] = Field(None, description="OAuth state parameter")+++class MCPOAuthSessionUpdate(BaseMCPOAuth):+ """Update an existing OAuth session."""++ authorization_url: Optional[str] = Field(None, description="OAuth authorization URL")+ authorization_code: Optional[str] = Field(None, description="OAuth authorization code")+ access_token: Optional[str] = Field(None, description="OAuth access token")+ refresh_token: Optional[str] = Field(None, description="OAuth refresh token")+ token_type: Optional[str] = Field(None, description="Token type")+ expires_at: Optional[datetime] = Field(None, description="Token expiry time")+ scope: Optional[str] = Field(None, description="OAuth scope")+ client_id: Optional[str] = Field(None, description="OAuth client ID")+ client_secret: Optional[str] = Field(None, description="OAuth client secret")+ redirect_uri: Optional[str] = Field(None, description="OAuth redirect URI")+ status: Optional[OAuthSessionStatus] = Field(None, description="Session status")+++class MCPServerResyncResult(LettaBase):+ """Result of resyncing MCP server tools."""++ deleted: List[str] = Field(default_factory=list, description="List of deleted tool names")+ updated: List[str] = Field(default_factory=list, description="List of updated tool names")+ added: List[str] = Field(default_factory=list, description="List of added tool names")+++class MCPToolExecuteRequest(LettaBase):+ """Request to execute an MCP tool by IDs."""++ args: Dict[str, Any] = Field(default_factory=dict, description="Arguments to pass to the MCP tool")+++def convert_generic_to_union(server) -> MCPServerUnion:+ """+ Convert a generic MCPServer (from letta.schemas.mcp) to the appropriate MCPServerUnion type+ based on the server_type field.++ This is used to convert internal MCPServer representations to the API response types.++ Args:+ server: A GenericMCPServer instance from letta.schemas.mcp++ Returns:+ The appropriate MCPServerUnion type (StdioMCPServer, SSEMCPServer, or StreamableHTTPMCPServer)+ """+ # Import here to avoid circular dependency+ from letta.schemas.mcp import MCPServer as GenericMCPServer++ if not isinstance(server, GenericMCPServer):+ raise TypeError(f"Expected GenericMCPServer, got {type(server)}")++ if server.server_type == MCPServerType.STDIO:+ return StdioMCPServer(+ id=server.id,+ server_name=server.server_name,+ type=MCPServerType.STDIO,+ command=server.stdio_config.command if server.stdio_config else None,+ args=server.stdio_config.args if server.stdio_config else None,+ env=server.stdio_config.env if server.stdio_config else None,+ )+ elif server.server_type == MCPServerType.SSE:+ return SSEMCPServer(+ id=server.id,+ server_name=server.server_name,+ type=MCPServerType.SSE,+ server_url=server.server_url,+ auth_header="Authorization" if server.token else None,+ auth_token=f"Bearer {server.token}" if server.token else None,+ custom_headers=server.custom_headers,+ )+ elif server.server_type == MCPServerType.STREAMABLE_HTTP:+ return StreamableHTTPMCPServer(+ id=server.id,+ server_name=server.server_name,+ type=MCPServerType.STREAMABLE_HTTP,+ server_url=server.server_url,+ auth_header="Authorization" if server.token else None,+ auth_token=f"Bearer {server.token}" if server.token else None,+ custom_headers=server.custom_headers,+ )+ else:+ raise ValueError(f"Unknown server type: {server.server_type}")diff --git a/letta/server/rest_api/routers/v1/mcp_servers.py b/letta/server/rest_api/routers/v1/mcp_servers.pynew file mode 100644index 000000000..e29c5a1d4--- /dev/null+++ b/letta/server/rest_api/routers/v1/mcp_servers.py@@ -0,0 +1,309 @@+from typing import Any, Dict, List, Optional++from fastapi import APIRouter, Body, Depends, HTTPException+from starlette.responses import StreamingResponse++from letta.log import get_logger+from letta.schemas.letta_message import ToolReturnMessage+from letta.schemas.mcp_server import (+ CreateMCPServerUnion,+ MCPServerUnion,+ MCPToolExecuteRequest,+ UpdateMCPServerUnion,+ convert_generic_to_union,+)+from letta.schemas.tool import Tool+from letta.server.rest_api.dependencies import (+ HeaderParams,+ get_headers,+ get_letta_server,+)+from letta.server.server import SyncServer+from letta.settings import tool_settings++router = APIRouter(prefix="/mcp-servers", tags=["mcp-servers"])++logger = get_logger(__name__)+++ "/",+ response_model=MCPServerUnion,+ operation_id="mcp_create_mcp_server",+)+async def create_mcp_server(+ request: CreateMCPServerUnion = Body(...),+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Add a new MCP server to the Letta MCP server config+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ new_server = await server.mcp_server_manager.create_mcp_server_from_config_with_tools(request, actor=actor)+ return convert_generic_to_union(new_server)+++ "/",+ response_model=List[MCPServerUnion],+ operation_id="mcp_list_mcp_servers",+)+async def list_mcp_servers(+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Get a list of all configured MCP servers+ """++ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ mcp_servers = await server.mcp_server_manager.list_mcp_servers(actor=actor)+ return [convert_generic_to_union(mcp_server) for mcp_server in mcp_servers]+++ "/{mcp_server_id}",+ response_model=MCPServerUnion,+ operation_id="mcp_get_mcp_server",+)+async def get_mcp_server(+ mcp_server_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Get a specific MCP server+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ current_server = await server.mcp_server_manager.get_mcp_server_by_id_async(mcp_server_id=mcp_server_id, actor=actor)+ return convert_generic_to_union(current_server)+++ "/{mcp_server_id}",+ status_code=204,+ operation_id="mcp_delete_mcp_server",+)+async def delete_mcp_server(+ mcp_server_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Delete an MCP server by its ID+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ await server.mcp_server_manager.delete_mcp_server_by_id(mcp_server_id, actor=actor)+++ "/{mcp_server_id}",+ response_model=MCPServerUnion,+ operation_id="mcp_update_mcp_server",+)+async def update_mcp_server(+ mcp_server_id: str,+ request: UpdateMCPServerUnion = Body(...),+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Update an existing MCP server configuration+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ updated_server = await server.mcp_server_manager.update_mcp_server_by_id(+ mcp_server_id=mcp_server_id, mcp_server_update=request, actor=actor+ )+ return convert_generic_to_union(updated_server)++[email protected]("/{mcp_server_id}/tools", response_model=List[Tool], operation_id="mcp_list_mcp_tools_by_server")+async def list_mcp_tools_by_server(+ mcp_server_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Get a list of all tools for a specific MCP server+ """+ # TODO: implement this. We want to use the new tools table instead of going to the mcp server.+ pass+ # actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ # mcp_tools = await server.mcp_server_manager.list_mcp_server_tools(mcp_server_id, actor=actor)+ # # Convert MCPTool objects to Tool objects+ # tools = []+ # for mcp_tool in mcp_tools:+ # from letta.schemas.tool import ToolCreate+ # tool_create = ToolCreate.from_mcp(mcp_server_name="", mcp_tool=mcp_tool)+ # tools.append(Tool(+ # id=f"mcp-tool-{mcp_tool.name}", # Generate a temporary ID+ # name=mcp_tool.name,+ # description=tool_create.description,+ # json_schema=tool_create.json_schema,+ # source_code=tool_create.source_code,+ # tags=tool_create.tags,+ # ))+ # return tools++[email protected]("/{mcp_server_id}/tools/{tool_id}", response_model=Tool, operation_id="mcp_get_mcp_tool")+async def get_mcp_tool(+ mcp_server_id: str,+ tool_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+):+ """+ Get a specific MCP tool by its ID+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ # Use the tool_manager's existing method to get the tool by ID+ # Verify the tool belongs to the MCP server (optional check)+ tool = await server.tool_manager.get_tool_by_id_async(tool_id=tool_id, actor=actor)+ return tool++[email protected]("/{mcp_server_id}/tools/{tool_id}/run", response_model=ToolReturnMessage, operation_id="mcp_run_tool")+async def run_mcp_tool(+ mcp_server_id: str,+ tool_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+ request: MCPToolExecuteRequest = Body(default=MCPToolExecuteRequest()),+):+ """+ Execute a specific MCP tool++ The request body should contain the tool arguments in the MCPToolExecuteRequest format.+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)++ # Execute the tool+ result, success = await server.mcp_server_manager.execute_mcp_server_tool(+ mcp_server_id=mcp_server_id,+ tool_id=tool_id,+ tool_args=request.args,+ environment_variables={}, # TODO: Get environment variables from somewhere if needed+ actor=actor,+ )++ # Create a ToolReturnMessage+ return ToolReturnMessage(+ id=f"tool-return-{tool_id}", tool_call_id=f"call-{tool_id}", tool_return=result, status="success" if success else "error"+ )++[email protected]("/{mcp_server_id}/refresh", operation_id="mcp_refresh_mcp_server_tools")+async def refresh_mcp_server_tools(+ mcp_server_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+ agent_id: Optional[str] = None,+):+ """+ Refresh tools for an MCP server by:+ 1. Fetching current tools from the MCP server+ 2. Deleting tools that no longer exist on the server+ 3. Updating schemas for existing tools+ 4. Adding new tools from the server++ Returns a summary of changes made.+ """+ actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)+ result = await server.mcp_server_manager.resync_mcp_server_tools(mcp_server_id, actor=actor, agent_id=agent_id)+ return result+++ "/connect/{mcp_server_id}",+ response_model=None,+ # TODO: make this into a model?+ responses={+ 200: {+ "description": "Successful response",+ "content": {+ "text/event-stream": {"description": "Server-Sent Events stream"},+ },+ }+ },+ operation_id="mcp_connect_mcp_server",+)+async def connect_mcp_server(+ mcp_server_id: str,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),+) -> StreamingResponse:+ """+ Connect to an MCP server with support for OAuth via SSE.+ Returns a stream of events handling authorization state and exchange if OAuth is required.+ """+ pass++ # async def oauth_stream_generator(+ # request: Union[StdioServerConfig, SSEServerConfig, StreamableHTTPServerConfig],+ # http_request: Request,+ # ) -> AsyncGenerator[str, None]:+ # client = None++ # oauth_flow_attempted = False+ # try:+ # # Acknolwedge connection attempt+ # yield oauth_stream_event(OauthStreamEvent.CONNECTION_ATTEMPT, server_name=request.server_name)++ # actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)++ # # Create MCP client with respective transport type+ # try:+ # request.resolve_environment_variables()+ # client = await server.mcp_server_manager.get_mcp_client(request, actor)+ # except ValueError as e:+ # yield oauth_stream_event(OauthStreamEvent.ERROR, message=str(e))+ # return++ # # Try normal connection first for flows that don't require OAuth+ # try:+ # await client.connect_to_server()+ # tools = await client.list_tools(serialize=True)+ # yield oauth_stream_event(OauthStreamEvent.SUCCESS, tools=tools)+ # return+ # except ConnectionError:+ # # TODO: jnjpng make this connection error check more specific to the 401 unauthorized error+ # if isinstance(client, AsyncStdioMCPClient):+ # logger.warning("OAuth not supported for stdio")+ # yield oauth_stream_event(OauthStreamEvent.ERROR, message="OAuth not supported for stdio")+ # return+ # # Continue to OAuth flow+ # logger.info(f"Attempting OAuth flow for {request}...")+ # except Exception as e:+ # yield oauth_stream_event(OauthStreamEvent.ERROR, message=f"Connection failed: {str(e)}")+ # return+ # finally:+ # if client:+ # try:+ # await client.cleanup()+ # # This is a workaround to catch the expected 401 Unauthorized from the official MCP SDK, see their streamable_http.py+ # # For SSE transport types, we catch the ConnectionError above, but Streamable HTTP doesn't bubble up the exception+ # except* HTTPStatusError:+ # oauth_flow_attempted = True+ # async for event in server.mcp_server_manager.handle_oauth_flow(request=request, actor=actor, http_request=http_request):+ # yield event++ # # Failsafe to make sure we don't try to handle OAuth flow twice+ # if not oauth_flow_attempted:+ # async for event in server.mcp_server_manager.handle_oauth_flow(request=request, actor=actor, http_request=http_request):+ # yield event+ # return+ # except Exception as e:+ # detailed_error = drill_down_exception(e)+ # logger.error(f"Error in OAuth stream:\n{detailed_error}")+ # yield oauth_stream_event(OauthStreamEvent.ERROR, message=f"Internal error: {detailed_error}")++ # finally:+ # if client:+ # try:+ # await client.cleanup()+ # except Exception as cleanup_error:+ # logger.warning(f"Error during temp MCP client cleanup: {cleanup_error}")++ # return StreamingResponseWithStatusCode(oauth_stream_generator(request, http_request), media_type="text/event-stream")