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.

feat: make config for mcp_servers nested [LET-6098] (#6155)

Ari Webb0884272d1c15
diff --git a/fern/openapi-overrides.yml b/fern/openapi-overrides.yml
index 19561722f..cc7bf1535 100644
--- a/fern/openapi-overrides.yml
+++ b/fern/openapi-overrides.yml
@@ -6,7 +6,49 @@ servers:
description: Self-hosted
x-fern-server-name: Self-hosted
+components:
+ schemas:
+ UpdateMCPServerRequest:
+ x-fern-ignore: true
+ CreateMCPServerRequest:
+ x-fern-ignore: true
+ StdioMCPServer:
+ x-fern-ignore: true
+ SSEMCPServer:
+ x-fern-ignore: true
+ StreamableHTTPMCPServer:
+ x-fern-ignore: true
+
paths:
+ /v1/mcp-servers/:
+ get:
+ x-fern-ignore: true
+ post:
+ x-fern-ignore: true
+ /v1/mcp-servers/{mcp_server_id}:
+ get:
+ x-fern-ignore: true
+ patch:
+ x-fern-ignore: true
+ delete:
+ x-fern-ignore: true
+ /v1/mcp-servers/{mcp_server_id}/tools:
+ get:
+ x-fern-ignore: true
+ /v1/mcp-servers/{mcp_server_id}/tools/{tool_id}:
+ get:
+ x-fern-ignore: true
+ /v1/mcp-servers/{mcp_server_id}/tools/{tool_id}/run:
+ post:
+ x-fern-ignore: true
+ /v1/mcp-servers/{mcp_server_id}/refresh:
+ post:
+ x-fern-ignore: true
+ patch:
+ x-fern-ignore: true
+ /v1/mcp-servers/connect/{mcp_server_id}:
+ post:
+ x-fern-ignore: true
/v1/tools/{tool_id}:
get:
x-fern-sdk-group-name:
diff --git a/fern/openapi.json b/fern/openapi.json
index 224a1d31a..1ef863f73 100644
--- a/fern/openapi.json
+++ b/fern/openapi.json
@@ -11400,18 +11400,7 @@
"content": {
"application/json": {
"schema": {
- "anyOf": [
- {
- "$ref": "#/components/schemas/CreateStdioMCPServer"
- },
- {
- "$ref": "#/components/schemas/CreateSSEMCPServer"
- },
- {
- "$ref": "#/components/schemas/CreateStreamableHTTPMCPServer"
- }
- ],
- "title": "Request"
+ "$ref": "#/components/schemas/CreateMCPServerRequest"
}
}
}
@@ -11598,18 +11587,7 @@
"content": {
"application/json": {
"schema": {
- "anyOf": [
- {
- "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateStdioMCPServer"
- },
- {
- "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateSSEMCPServer"
- },
- {
- "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateStreamableHTTPMCPServer"
- }
- ],
- "title": "Request"
+ "$ref": "#/components/schemas/UpdateMCPServerRequest"
}
}
}
@@ -24028,15 +24006,49 @@
"title": "CreateBlock",
"description": "Create a block"
},
- "CreateSSEMCPServer": {
+ "CreateMCPServerRequest": {
"properties": {
"server_name": {
"type": "string",
"title": "Server Name",
- "description": "The name of the server"
+ "description": "The name of the MCP server"
},
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "config": {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/CreateStdioMCPServer"
+ },
+ {
+ "$ref": "#/components/schemas/CreateSSEMCPServer"
+ },
+ {
+ "$ref": "#/components/schemas/CreateStreamableHTTPMCPServer"
+ }
+ ],
+ "title": "Config",
+ "description": "The MCP server configuration (Stdio, SSE, or Streamable HTTP)",
+ "discriminator": {
+ "propertyName": "mcp_server_type",
+ "mapping": {
+ "sse": "#/components/schemas/CreateSSEMCPServer",
+ "stdio": "#/components/schemas/CreateStdioMCPServer",
+ "streamable_http": "#/components/schemas/CreateStreamableHTTPMCPServer"
+ }
+ }
+ }
+ },
+ "additionalProperties": false,
+ "type": "object",
+ "required": ["server_name", "config"],
+ "title": "CreateMCPServerRequest",
+ "description": "Request to create a new MCP server with configuration."
+ },
+ "CreateSSEMCPServer": {
+ "properties": {
+ "mcp_server_type": {
+ "type": "string",
+ "const": "sse",
+ "title": "Mcp Server Type",
"default": "sse"
},
"server_url": {
@@ -24084,20 +24096,18 @@
"description": "Custom HTTP headers to include with requests"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "server_url"],
+ "required": ["server_url"],
"title": "CreateSSEMCPServer",
"description": "Create a new SSE MCP server"
},
"CreateStdioMCPServer": {
"properties": {
- "server_name": {
+ "mcp_server_type": {
"type": "string",
- "title": "Server Name",
- "description": "The name of the server"
- },
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "const": "stdio",
+ "title": "Mcp Server Type",
"default": "stdio"
},
"command": {
@@ -24129,20 +24139,18 @@
"description": "Environment variables to set"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "command", "args"],
+ "required": ["command", "args"],
"title": "CreateStdioMCPServer",
"description": "Create a new Stdio MCP server"
},
"CreateStreamableHTTPMCPServer": {
"properties": {
- "server_name": {
+ "mcp_server_type": {
"type": "string",
- "title": "Server Name",
- "description": "The name of the server"
- },
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "const": "streamable_http",
+ "title": "Mcp Server Type",
"default": "streamable_http"
},
"server_url": {
@@ -24190,8 +24198,9 @@
"description": "Custom HTTP headers to include with requests"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "server_url"],
+ "required": ["server_url"],
"title": "CreateStreamableHTTPMCPServer",
"description": "Create a new Streamable HTTP MCP server"
},
@@ -32659,13 +32668,10 @@
},
"SSEMCPServer": {
"properties": {
- "server_name": {
+ "mcp_server_type": {
"type": "string",
- "title": "Server Name",
- "description": "The name of the server"
- },
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "const": "sse",
+ "title": "Mcp Server Type",
"default": "sse"
},
"server_url": {
@@ -32718,10 +32724,16 @@
"title": "Id",
"description": "The human-friendly ID of the Mcp_server",
"examples": ["mcp_server-123e4567-e89b-12d3-a456-426614174000"]
+ },
+ "server_name": {
+ "type": "string",
+ "title": "Server Name",
+ "description": "The name of the MCP server"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "server_url"],
+ "required": ["server_url", "server_name"],
"title": "SSEMCPServer",
"description": "An SSE MCP server"
},
@@ -33563,13 +33575,10 @@
},
"StdioMCPServer": {
"properties": {
- "server_name": {
+ "mcp_server_type": {
"type": "string",
- "title": "Server Name",
- "description": "The name of the server"
- },
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "const": "stdio",
+ "title": "Mcp Server Type",
"default": "stdio"
},
"command": {
@@ -33606,10 +33615,16 @@
"title": "Id",
"description": "The human-friendly ID of the Mcp_server",
"examples": ["mcp_server-123e4567-e89b-12d3-a456-426614174000"]
+ },
+ "server_name": {
+ "type": "string",
+ "title": "Server Name",
+ "description": "The name of the MCP server"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "command", "args"],
+ "required": ["command", "args", "server_name"],
"title": "StdioMCPServer",
"description": "A Stdio MCP server"
},
@@ -34112,13 +34127,10 @@
},
"StreamableHTTPMCPServer": {
"properties": {
- "server_name": {
+ "mcp_server_type": {
"type": "string",
- "title": "Server Name",
- "description": "The name of the server"
- },
- "type": {
- "$ref": "#/components/schemas/MCPServerType",
+ "const": "streamable_http",
+ "title": "Mcp Server Type",
"default": "streamable_http"
},
"server_url": {
@@ -34171,10 +34183,16 @@
"title": "Id",
"description": "The human-friendly ID of the Mcp_server",
"examples": ["mcp_server-123e4567-e89b-12d3-a456-426614174000"]
+ },
+ "server_name": {
+ "type": "string",
+ "title": "Server Name",
+ "description": "The name of the MCP server"
}
},
+ "additionalProperties": false,
"type": "object",
- "required": ["server_name", "server_url"],
+ "required": ["server_url", "server_name"],
"title": "StreamableHTTPMCPServer",
"description": "A Streamable HTTP MCP server"
},
@@ -36621,6 +36639,50 @@
"required": ["content"],
"title": "UpdateAssistantMessage"
},
+ "UpdateMCPServerRequest": {
+ "properties": {
+ "server_name": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Server Name",
+ "description": "The name of the MCP server"
+ },
+ "config": {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateStdioMCPServer"
+ },
+ {
+ "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateSSEMCPServer"
+ },
+ {
+ "$ref": "#/components/schemas/letta__schemas__mcp_server__UpdateStreamableHTTPMCPServer"
+ }
+ ],
+ "title": "Config",
+ "description": "The MCP server configuration updates (Stdio, SSE, or Streamable HTTP)",
+ "discriminator": {
+ "propertyName": "mcp_server_type",
+ "mapping": {
+ "sse": "#/components/schemas/letta__schemas__mcp_server__UpdateSSEMCPServer",
+ "stdio": "#/components/schemas/letta__schemas__mcp_server__UpdateStdioMCPServer",
+ "streamable_http": "#/components/schemas/letta__schemas__mcp_server__UpdateStreamableHTTPMCPServer"
+ }
+ }
+ }
+ },
+ "additionalProperties": false,
+ "type": "object",
+ "required": ["config"],
+ "title": "UpdateMCPServerRequest",
+ "description": "Request to update an existing MCP server configuration."
+ },
"UpdateReasoningMessage": {
"properties": {
"reasoning": {
@@ -38562,17 +38624,11 @@
},
"letta__schemas__mcp_server__UpdateSSEMCPServer": {
"properties": {
- "server_name": {
- "anyOf": [
- {
- "type": "string"
- },
- {
- "type": "null"
- }
- ],
- "title": "Server Name",
- "description": "The name of the MCP server"
+ "mcp_server_type": {
+ "type": "string",
+ "const": "sse",
+ "title": "Mcp Server Type",
+ "default": "sse"
},
"server_url": {
"anyOf": [
@@ -38584,21 +38640,9 @@
}
],
"title": "Server Url",
- "description": "The URL of the SSE MCP server"
- },
- "auth_token": {
- "anyOf": [
- {
- "type": "string"
- },
- {
- "type": "null"
- }
- ],
- "title": "Auth Token",
- "description": "The authentication token or API key value"
+ "description": "The URL of the server"
},
- "token": {
+ "auth_header": {
"anyOf": [
{
"type": "string"
@@ -38607,10 +38651,10 @@
"type": "null"
}
],
- "title": "Token",
- "description": "The authentication token (internal)"
+ "title": "Auth Header",
+ "description": "The name of the authentication header (e.g., 'Authorization')"
},
- "auth_header": {
+ "auth_token": {
"anyOf": [
{
"type": "string"
@@ -38619,8 +38663,8 @@
"type": "null"
}
],
- "title": "Auth Header",
- "description": "The name of the authentication header (e.g., 'Authorization')"
+ "title": "Auth Token",
+ "description": "The authentication token or API key value"
},
"custom_headers": {
"anyOf": [
@@ -38635,27 +38679,22 @@
}
],
"title": "Custom Headers",
- "description": "Custom headers to send with requests"
+ "description": "Custom HTTP headers to include with requests"
}
},
"additionalProperties": false,
"type": "object",
+ "required": ["server_url"],
"title": "UpdateSSEMCPServer",
"description": "Update schema for SSE MCP server - all fields optional"
},
"letta__schemas__mcp_server__UpdateStdioMCPServer": {
"properties": {
- "server_name": {
- "anyOf": [
- {
- "type": "string"
- },
- {
- "type": "null"
- }
- ],
- "title": "Server Name",
- "description": "The name of the MCP server"
+ "mcp_server_type": {
+ "type": "string",
+ "const": "stdio",
+ "title": "Mcp Server Type",
+ "default": "stdio"
},
"command": {
"anyOf": [
@@ -38667,7 +38706,7 @@
}
],
"title": "Command",
- "description": "The command to run the MCP server"
+ "description": "The command to run (MCP 'local' client will run this command)"
},
"args": {
"anyOf": [
@@ -38702,22 +38741,17 @@
},
"additionalProperties": false,
"type": "object",
+ "required": ["command", "args"],
"title": "UpdateStdioMCPServer",
"description": "Update schema for Stdio MCP server - all fields optional"
},
"letta__schemas__mcp_server__UpdateStreamableHTTPMCPServer": {
"properties": {
- "server_name": {
- "anyOf": [
- {
- "type": "string"
- },
- {
- "type": "null"
- }
- ],
- "title": "Server Name",
- "description": "The name of the MCP server"
+ "mcp_server_type": {
+ "type": "string",
+ "const": "streamable_http",
+ "title": "Mcp Server Type",
+ "default": "streamable_http"
},
"server_url": {
"anyOf": [
@@ -38729,21 +38763,9 @@
}
],
"title": "Server Url",
- "description": "The URL of the Streamable HTTP MCP server"
- },
- "auth_token": {
- "anyOf": [
- {
- "type": "string"
- },
- {
- "type": "null"
- }
- ],
- "title": "Auth Token",
- "description": "The authentication token or API key value"
+ "description": "The URL of the server"
},
- "token": {
+ "auth_header": {
"anyOf": [
{
"type": "string"
@@ -38752,10 +38774,10 @@
"type": "null"
}
],
- "title": "Token",
- "description": "The authentication token (internal)"
+ "title": "Auth Header",
+ "description": "The name of the authentication header (e.g., 'Authorization')"
},
- "auth_header": {
+ "auth_token": {
"anyOf": [
{
"type": "string"
@@ -38764,8 +38786,8 @@
"type": "null"
}
],
- "title": "Auth Header",
- "description": "The name of the authentication header (e.g., 'Authorization')"
+ "title": "Auth Token",
+ "description": "The authentication token or API key value"
},
"custom_headers": {
"anyOf": [
@@ -38780,11 +38802,12 @@
}
],
"title": "Custom Headers",
- "description": "Custom headers to send with requests"
+ "description": "Custom HTTP headers to include with requests"
}
},
"additionalProperties": false,
"type": "object",
+ "required": ["server_url"],
"title": "UpdateStreamableHTTPMCPServer",
"description": "Update schema for Streamable HTTP MCP server - all fields optional"
},
diff --git a/letta/schemas/mcp_server.py b/letta/schemas/mcp_server.py
index d0364a5f9..49f73f952 100644
--- a/letta/schemas/mcp_server.py
+++ b/letta/schemas/mcp_server.py
@@ -1,6 +1,6 @@
import json
from datetime import datetime
-from typing import Any, Dict, List, Optional, Union
+from typing import Annotated, Any, Dict, List, Literal, Optional, Union
from pydantic import Field
@@ -23,17 +23,34 @@ class BaseMCPServer(LettaBase):
# Create Schemas (for POST requests)
-class CreateStdioMCPServer(StdioServerConfig):
+class CreateStdioMCPServer(LettaBase):
"""Create a new Stdio MCP server"""
+ mcp_server_type: Literal[MCPServerType.STDIO] = MCPServerType.STDIO
+ command: str = Field(..., description="The command to run (MCP 'local' client will run this command)")
+ args: List[str] = Field(..., description="The arguments to pass to the command")
+ env: Optional[dict[str, str]] = Field(None, description="Environment variables to set")
-class CreateSSEMCPServer(SSEServerConfig):
+
+class CreateSSEMCPServer(LettaBase):
"""Create a new SSE MCP server"""
+ mcp_server_type: Literal[MCPServerType.SSE] = MCPServerType.SSE
+ 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 requests")
-class CreateStreamableHTTPMCPServer(StreamableHTTPServerConfig):
+
+class CreateStreamableHTTPMCPServer(LettaBase):
"""Create a new Streamable HTTP MCP server"""
+ mcp_server_type: Literal[MCPServerType.STREAMABLE_HTTP] = MCPServerType.STREAMABLE_HTTP
+ 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 requests")
+
CreateMCPServerUnion = Union[CreateStdioMCPServer, CreateSSEMCPServer, CreateStreamableHTTPMCPServer]
@@ -42,21 +59,21 @@ class StdioMCPServer(CreateStdioMCPServer):
"""A Stdio MCP server"""
id: str = BaseMCPServer.generate_id_field()
- type: MCPServerType = MCPServerType.STDIO
+ server_name: str = Field(..., description="The name of the MCP server")
class SSEMCPServer(CreateSSEMCPServer):
"""An SSE MCP server"""
id: str = BaseMCPServer.generate_id_field()
- type: MCPServerType = MCPServerType.SSE
+ server_name: str = Field(..., description="The name of the MCP server")
class StreamableHTTPMCPServer(CreateStreamableHTTPMCPServer):
"""A Streamable HTTP MCP server"""
id: str = BaseMCPServer.generate_id_field()
- type: MCPServerType = MCPServerType.STREAMABLE_HTTP
+ server_name: str = Field(..., description="The name of the MCP server")
MCPServerUnion = Union[StdioMCPServer, SSEMCPServer, StreamableHTTPMCPServer]
@@ -67,34 +84,30 @@ MCPServerUnion = Union[StdioMCPServer, SSEMCPServer, StreamableHTTPMCPServer]
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")
+ mcp_server_type: Literal[MCPServerType.STDIO] = MCPServerType.STDIO
+ command: Optional[str] = Field(..., description="The command to run (MCP 'local' client will run this command)")
+ args: Optional[List[str]] = Field(..., 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")
- # Accept both `auth_token` (API surface) and `token` (internal ORM naming)
- auth_token: Optional[str] = Field(None, description="The authentication token or API key value")
- token: Optional[str] = Field(None, description="The authentication token (internal)")
+ mcp_server_type: Literal[MCPServerType.SSE] = MCPServerType.SSE
+ server_url: Optional[str] = Field(..., description="The URL of the server")
auth_header: Optional[str] = Field(None, description="The name of the authentication header (e.g., 'Authorization')")
- custom_headers: Optional[Dict[str, str]] = Field(None, description="Custom headers to send with requests")
+ 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 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")
- # Accept both `auth_token` (API surface) and `token` (internal ORM naming)
- auth_token: Optional[str] = Field(None, description="The authentication token or API key value")
- token: Optional[str] = Field(None, description="The authentication token (internal)")
+ mcp_server_type: Literal[MCPServerType.STREAMABLE_HTTP] = MCPServerType.STREAMABLE_HTTP
+ server_url: Optional[str] = Field(..., description="The URL of the server")
auth_header: Optional[str] = Field(None, description="The name of the authentication header (e.g., 'Authorization')")
- custom_headers: Optional[Dict[str, str]] = Field(None, description="Custom headers to send with requests")
+ 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 requests")
UpdateMCPServerUnion = Union[UpdateStdioMCPServer, UpdateSSEMCPServer, UpdateStreamableHTTPMCPServer]
@@ -252,6 +265,27 @@ class MCPToolExecuteRequest(LettaBase):
args: Dict[str, Any] = Field(default_factory=dict, description="Arguments to pass to the MCP tool")
+# Wrapper models for API requests with discriminated unions
+class CreateMCPServerRequest(LettaBase):
+ """Request to create a new MCP server with configuration."""
+
+ server_name: str = Field(..., description="The name of the MCP server")
+ config: Annotated[
+ CreateMCPServerUnion,
+ Field(..., discriminator="mcp_server_type", description="The MCP server configuration (Stdio, SSE, or Streamable HTTP)"),
+ ]
+
+
+class UpdateMCPServerRequest(LettaBase):
+ """Request to update an existing MCP server configuration."""
+
+ server_name: Optional[str] = Field(None, description="The name of the MCP server")
+ config: Annotated[
+ UpdateMCPServerUnion,
+ Field(..., discriminator="mcp_server_type", description="The MCP server configuration updates (Stdio, SSE, or Streamable HTTP)"),
+ ]
+
+
def convert_generic_to_union(server) -> MCPServerUnion:
"""
Convert a generic MCPServer (from letta.schemas.mcp) to the appropriate MCPServerUnion type
@@ -275,7 +309,7 @@ def convert_generic_to_union(server) -> MCPServerUnion:
return StdioMCPServer(
id=server.id,
server_name=server.server_name,
- type=MCPServerType.STDIO,
+ mcp_server_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,
@@ -284,7 +318,7 @@ def convert_generic_to_union(server) -> MCPServerUnion:
return SSEMCPServer(
id=server.id,
server_name=server.server_name,
- type=MCPServerType.SSE,
+ mcp_server_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,
@@ -294,7 +328,7 @@ def convert_generic_to_union(server) -> MCPServerUnion:
return StreamableHTTPMCPServer(
id=server.id,
server_name=server.server_name,
- type=MCPServerType.STREAMABLE_HTTP,
+ mcp_server_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,
@@ -304,12 +338,27 @@ def convert_generic_to_union(server) -> MCPServerUnion:
raise ValueError(f"Unknown server type: {server.server_type}")
-def convert_update_to_internal(request: Union[UpdateStdioMCPServer, UpdateSSEMCPServer, UpdateStreamableHTTPMCPServer]):
- """Convert external API update models to internal UpdateMCPServer union used by the manager.
+def convert_update_to_internal(request: UpdateMCPServerRequest):
+ """Convert external UpdateMCPServerRequest to internal UpdateMCPServer union used by the manager.
- - Flattens stdio fields into StdioServerConfig inside UpdateStdioMCPServer
- - Maps `auth_token` to `token` for HTTP-based transports
- - Ignores `auth_header` at update time (header is derived from token)
+ External API Request Structure (UpdateMCPServerRequest):
+ - server_name: Optional[str] (at top level)
+ - config: UpdateMCPServerUnion
+ - UpdateStdioMCPServer: command, args, env (flat fields)
+ - UpdateSSEMCPServer: server_url, auth_header, auth_token, custom_headers
+ - UpdateStreamableHTTPMCPServer: server_url, auth_header, auth_token, custom_headers
+
+ Internal Layer (schemas/mcp.py):
+ - UpdateStdioMCPServer: server_name, stdio_config (wrapped in StdioServerConfig)
+ - UpdateSSEMCPServer: server_name, server_url, token (not auth_token!), custom_headers
+ - UpdateStreamableHTTPMCPServer: server_name, server_url, auth_header, auth_token, custom_headers
+
+ This function:
+ 1. Extracts server_name from request (top level)
+ 2. Wraps stdio fields into StdioServerConfig
+ 3. Maps auth_token → token for SSE (internal uses 'token')
+ 4. Passes through auth_header + auth_token for StreamableHTTP
+ 5. Strips 'Bearer ' prefix from tokens if present
"""
# Local import to avoid circulars
from letta.functions.mcp_client.types import MCPServerType as MCPType, StdioServerConfig as StdioCfg
@@ -319,32 +368,78 @@ def convert_update_to_internal(request: Union[UpdateStdioMCPServer, UpdateSSEMCP
UpdateStreamableHTTPMCPServer as InternalUpdateHTTP,
)
- if isinstance(request, UpdateStdioMCPServer):
+ config = request.config
+ server_name = request.server_name
+
+ if isinstance(config, UpdateStdioMCPServer):
+ # For Stdio: wrap command/args/env into StdioServerConfig
stdio_cfg = None
- # Only build stdio_config if command and args are explicitly provided to avoid overwriting existing config
- if request.command is not None and request.args is not None:
+ # Only build stdio_config if command and args are explicitly provided
+ if config.command is not None and config.args is not None:
+ # Note: server_name in StdioServerConfig should match the parent server's name
+ # Use empty string as placeholder if server_name update is not provided
stdio_cfg = StdioCfg(
- server_name=request.server_name or "",
+ server_name=server_name or "", # Will be overwritten by manager if needed
type=MCPType.STDIO,
- command=request.command,
- args=request.args,
- env=request.env,
+ command=config.command,
+ args=config.args,
+ env=config.env,
)
+
+ # Build kwargs with only non-None values
kwargs: dict = {}
- if request.server_name is not None:
- kwargs["server_name"] = request.server_name
+ if server_name is not None:
+ kwargs["server_name"] = server_name
if stdio_cfg is not None:
kwargs["stdio_config"] = stdio_cfg
+
return InternalUpdateStdio(**kwargs)
- elif isinstance(request, UpdateSSEMCPServer):
- token_value = request.auth_token or request.token
- return InternalUpdateSSE(
- server_name=request.server_name, server_url=request.server_url, token=token_value, custom_headers=request.custom_headers
- )
- elif isinstance(request, UpdateStreamableHTTPMCPServer):
- token_value = request.auth_token or request.token
- return InternalUpdateHTTP(
- server_name=request.server_name, server_url=request.server_url, auth_token=token_value, custom_headers=request.custom_headers
- )
+
+ elif isinstance(config, UpdateSSEMCPServer):
+ # For SSE: map auth_token → token, strip Bearer prefix if present
+ token_value = None
+ if config.auth_token is not None:
+ # Strip 'Bearer ' prefix if present (internal storage doesn't include prefix)
+ token_value = config.auth_token
+ if token_value.startswith(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} "):
+ token_value = token_value[len(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} ") :]
+
+ # Build kwargs with only non-None values
+ kwargs: dict = {}
+ if server_name is not None:
+ kwargs["server_name"] = server_name
+ if config.server_url is not None:
+ kwargs["server_url"] = config.server_url
+ if token_value is not None:
+ kwargs["token"] = token_value
+ if config.custom_headers is not None:
+ kwargs["custom_headers"] = config.custom_headers
+
+ return InternalUpdateSSE(**kwargs)
+
+ elif isinstance(config, UpdateStreamableHTTPMCPServer):
+ # For StreamableHTTP: pass through auth_header + auth_token, strip Bearer prefix if present
+ auth_token_value = None
+ if config.auth_token is not None:
+ # Strip 'Bearer ' prefix if present (internal storage doesn't include prefix)
+ auth_token_value = config.auth_token
+ if auth_token_value.startswith(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} "):
+ auth_token_value = auth_token_value[len(f"{MCP_AUTH_TOKEN_BEARER_PREFIX} ") :]
+
+ # Build kwargs with only non-None values
+ kwargs: dict = {}
+ if server_name is not None:
+ kwargs["server_name"] = server_name
+ if config.server_url is not None:
+ kwargs["server_url"] = config.server_url
+ if config.auth_header is not None:
+ kwargs["auth_header"] = config.auth_header
+ if auth_token_value is not None:
+ kwargs["auth_token"] = auth_token_value
+ if config.custom_headers is not None:
+ kwargs["custom_headers"] = config.custom_headers
+
+ return InternalUpdateHTTP(**kwargs)
+
else:
- raise TypeError(f"Unsupported update request type: {type(request)}")
+ raise TypeError(f"Unsupported update config type: {type(config)}")
diff --git a/letta/server/rest_api/routers/v1/mcp_servers.py b/letta/server/rest_api/routers/v1/mcp_servers.py
index 723a0ffcf..9a8e1b80f 100644
--- a/letta/server/rest_api/routers/v1/mcp_servers.py
+++ b/letta/server/rest_api/routers/v1/mcp_servers.py
@@ -8,10 +8,10 @@ from letta.functions.mcp_client.types import SSEServerConfig, StdioServerConfig,
from letta.log import get_logger
from letta.schemas.letta_message import ToolReturnMessage
from letta.schemas.mcp_server import (
- CreateMCPServerUnion,
+ CreateMCPServerRequest,
MCPServerUnion,
MCPToolExecuteRequest,
- UpdateMCPServerUnion,
+ UpdateMCPServerRequest,
convert_generic_to_union,
convert_update_to_internal,
)
@@ -40,7 +40,7 @@ logger = get_logger(__name__)
operation_id="mcp_create_mcp_server",
)
async def create_mcp_server(
- request: CreateMCPServerUnion = Body(...),
+ request: CreateMCPServerRequest = Body(...),
server: SyncServer = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
):
@@ -49,7 +49,7 @@ async def create_mcp_server(
"""
# TODO: add the tools to the MCP server table we made.
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)
+ new_server = await server.mcp_server_manager.create_mcp_server_from_request(request, actor=actor)
return convert_generic_to_union(new_server)
@@ -112,7 +112,7 @@ async def delete_mcp_server(
)
async def update_mcp_server(
mcp_server_id: str,
- request: UpdateMCPServerUnion = Body(...),
+ request: UpdateMCPServerRequest = Body(...),
server: SyncServer = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
):
diff --git a/letta/services/mcp_server_manager.py b/letta/services/mcp_server_manager.py
index 7f71d828d..08639a130 100644
--- a/letta/services/mcp_server_manager.py
+++ b/letta/services/mcp_server_manager.py
@@ -36,6 +36,7 @@ from letta.schemas.mcp import (
UpdateStdioMCPServer,
UpdateStreamableHTTPMCPServer,
)
+from letta.schemas.mcp_server import CreateMCPServerRequest, CreateSSEMCPServer, CreateStdioMCPServer, CreateStreamableHTTPMCPServer
from letta.schemas.secret import Secret
from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
from letta.schemas.user import User as PydanticUser
@@ -545,6 +546,29 @@ class MCPServerManager:
return mcp_server
+ @enforce_types
+ async def create_mcp_server_from_request(self, request: CreateMCPServerRequest, actor: PydanticUser) -> MCPServer:
+ """
+ Create an MCP server from a request object.
+ """
+ # Convert CreateMCPServerUnion to ServerConfig union by adding server_name
+ config_type_map = {
+ CreateStdioMCPServer: StdioServerConfig,
+ CreateSSEMCPServer: SSEServerConfig,
+ CreateStreamableHTTPMCPServer: StreamableHTTPServerConfig,
+ }
+
+ config_dict = request.config.model_dump(exclude={"mcp_server_type"})
+ config_dict["server_name"] = request.server_name
+ config_dict["type"] = request.config.mcp_server_type
+ server_config = config_type_map[type(request.config)](**config_dict)
+
+ # Create the MCP server object (with encryption of sensitive fields)
+ mcp_server = await self.create_mcp_server_from_config(server_config, actor)
+
+ # Persist to database and sync tools
+ return await self.create_mcp_server_with_tools(mcp_server, actor)
+
@enforce_types
async def create_mcp_server_from_config_with_tools(
self, server_config: Union[StdioServerConfig, SSEServerConfig, StreamableHTTPServerConfig], actor: PydanticUser
diff --git a/tests/sdk_v1/mcp_servers_test.py b/tests/sdk_v1/mcp_servers_test.py
index 59d767894..c8b7d1871 100644
--- a/tests/sdk_v1/mcp_servers_test.py
+++ b/tests/sdk_v1/mcp_servers_test.py
@@ -109,9 +109,11 @@ def mock_mcp_server_config_for_agent() -> Dict[str, Any]:
return {
"server_name": server_name,
- "type": "stdio",
- "command": sys.executable, # Use the current Python interpreter
- "args": [str(mcp_server_path)],
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": sys.executable, # Use the current Python interpreter
+ "args": [str(mcp_server_path)],
+ },
}
@@ -188,44 +190,50 @@ def get_attr(obj, attr):
def create_stdio_server_request(server_name: str, command: str = "npx", args: List[str] = None) -> Dict[str, Any]:
"""Create a stdio MCP server configuration object.
- Note: The SDK infers type="stdio" from the presence of args/command parameters.
+ Returns a dict with server_name and config following CreateMCPServerRequest schema.
"""
return {
"server_name": server_name,
- "command": command,
- "args": args or ["-y", "@modelcontextprotocol/server-everything"],
- "env": {"NODE_ENV": "test", "DEBUG": "true"},
- "type": "stdio", # Optional but explicit
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": command,
+ "args": args or ["-y", "@modelcontextprotocol/server-everything"],
+ "env": {"NODE_ENV": "test", "DEBUG": "true"},
+ },
}
def create_sse_server_request(server_name: str, server_url: str = None) -> Dict[str, Any]:
"""Create an SSE MCP server configuration object.
- Note: The SDK infers type="sse" from the presence of server_url parameter.
+ Returns a dict with server_name and config following CreateMCPServerRequest schema.
"""
return {
"server_name": server_name,
- "server_url": server_url or "https://api.example.com/sse",
- "auth_header": "Authorization",
- "auth_token": "Bearer test_token_123",
- "custom_headers": {"X-Custom-Header": "custom_value", "X-API-Version": "1.0"},
- "type": "sse", # Optional but explicit
+ "config": {
+ "mcp_server_type": "sse",
+ "server_url": server_url or "https://api.example.com/sse",
+ "auth_header": "Authorization",
+ "auth_token": "Bearer test_token_123",
+ "custom_headers": {"X-Custom-Header": "custom_value", "X-API-Version": "1.0"},
+ },
}
def create_streamable_http_server_request(server_name: str, server_url: str = None) -> Dict[str, Any]:
"""Create a streamable HTTP MCP server configuration object.
- Note: The SDK infers type="streamable_http" from the presence of server_url parameter.
+ Returns a dict with server_name and config following CreateMCPServerRequest schema.
"""
return {
"server_name": server_name,
- "server_url": server_url or "https://api.example.com/streamable",
- "auth_header": "X-API-Key",
- "auth_token": "api_key_456",
- "custom_headers": {"Accept": "application/json", "X-Version": "2.0"},
- "type": "streamable_http", # Optional but explicit
+ "config": {
+ "mcp_server_type": "streamable_http",
+ "server_url": server_url or "https://api.example.com/streamable",
+ "auth_header": "X-API-Key",
+ "auth_token": "api_key_456",
+ "custom_headers": {"Accept": "application/json", "X-Version": "2.0"},
+ },
}
@@ -233,13 +241,15 @@ def create_exa_streamable_http_server_request(server_name: str) -> Dict[str, Any
"""Create a Streamable HTTP config for Exa MCP with no auth.
Reference: https://mcp.exa.ai/mcp
- Note: The SDK infers type="streamable_http" from the presence of server_url parameter.
+ Returns a dict with server_name and config following CreateMCPServerRequest schema.
"""
return {
"server_name": server_name,
- "server_url": "https://mcp.exa.ai/mcp?exaApiKey=your-exa-api-key",
- "type": "streamable_http", # Optional but explicit
- # no auth header/token, no custom headers
+ "config": {
+ "mcp_server_type": "streamable_http",
+ "server_url": "https://mcp.exa.ai/mcp?exaApiKey=your-exa-api-key",
+ # no auth header/token, no custom headers
+ },
}
@@ -257,16 +267,17 @@ def test_create_stdio_mcp_server(client: Letta):
server_data = client.mcp_servers.create(**server_config)
# Handle both dict and object attribute access
+ # Response should have server_name at top level and config fields flattened
if isinstance(server_data, dict):
assert server_data["server_name"] == server_name
- assert server_data["command"] == server_config["command"]
- assert server_data["args"] == server_config["args"]
+ assert server_data["command"] == server_config["config"]["command"]
+ assert server_data["args"] == server_config["config"]["args"]
assert server_data["id"] is not None # Should have an ID assigned
server_id = server_data["id"]
else:
assert server_data.server_name == server_name
- assert server_data.command == server_config["command"] # server_config is always a dict
- assert server_data.args == server_config["args"] # server_config is always a dict
+ assert server_data.command == server_config["config"]["command"]
+ assert server_data.args == server_config["config"]["args"]
assert server_data.id is not None # Should have an ID assigned
server_id = server_data.id
@@ -285,14 +296,14 @@ def test_create_sse_mcp_server(client: Letta):
# Handle both dict and object attribute access
if isinstance(server_data, dict):
assert server_data["server_name"] == server_name
- assert server_data["server_url"] == server_config["server_url"]
- assert server_data["auth_header"] == server_config["auth_header"]
+ assert server_data["server_url"] == server_config["config"]["server_url"]
+ assert server_data["auth_header"] == server_config["config"]["auth_header"]
assert server_data["id"] is not None
server_id = server_data["id"]
else:
assert server_data.server_name == server_name
- assert server_data.server_url == server_config["server_url"] # server_config is always a dict
- assert server_data.auth_header == server_config["auth_header"] # server_config is always a dict
+ assert server_data.server_url == server_config["config"]["server_url"]
+ assert server_data.auth_header == server_config["config"]["auth_header"]
assert server_data.id is not None
server_id = server_data.id
@@ -311,12 +322,12 @@ def test_create_streamable_http_mcp_server(client: Letta):
# Handle both dict and object attribute access
if isinstance(server_data, dict):
assert server_data["server_name"] == server_name
- assert server_data["server_url"] == server_config["server_url"]
+ assert server_data["server_url"] == server_config["config"]["server_url"]
assert server_data["id"] is not None
server_id = server_data["id"]
else:
assert server_data.server_name == server_name
- assert server_data.server_url == server_config["server_url"] # server_config is always a dict
+ assert server_data.server_url == server_config["config"]["server_url"]
assert server_data.id is not None
server_id = server_data.id
@@ -370,7 +381,7 @@ def test_get_specific_mcp_server(client: Letta):
# Create a server
server_name = f"get-test-{uuid.uuid4().hex[:8]}"
server_config = create_stdio_server_request(server_name, command="python", args=["-m", "mcp_server"])
- server_config["env"]["PYTHONPATH"] = "/usr/local/lib"
+ server_config["config"]["env"]["PYTHONPATH"] = "/usr/local/lib"
created_server = client.mcp_servers.create(**server_config)
server_id = get_attr(created_server, "id")
@@ -407,9 +418,12 @@ def test_update_stdio_mcp_server(client: Letta):
# Update the server
update_request = {
"server_name": "updated-stdio-server",
- "command": "node",
- "args": ["new_server.js", "--port", "3000"],
- "env": {"NEW_ENV": "new_value", "PORT": "3000"},
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": "node",
+ "args": ["new_server.js", "--port", "3000"],
+ "env": {"NEW_ENV": "new_value", "PORT": "3000"},
+ },
}
updated_server = client.mcp_servers.update(server_id, **update_request)
@@ -440,9 +454,12 @@ def test_update_sse_mcp_server(client: Letta):
# Update the server
update_request = {
"server_name": "updated-sse-server",
- "server_url": "https://new.example.com/sse/v2",
- "auth_token": "new_token_789",
- "custom_headers": {"X-Updated": "true", "X-Version": "2.0"},
+ "config": {
+ "mcp_server_type": "sse",
+ "server_url": "https://new.example.com/sse/v2",
+ "auth_token": "new_token_789",
+ "custom_headers": {"X-Updated": "true", "X-Version": "2.0"},
+ },
}
updated_server = client.mcp_servers.update(server_id, **update_request)
@@ -561,13 +578,13 @@ def test_multiple_server_types_coexist(client: Letta):
# Get each server and verify type-specific fields
stdio_retrieved = client.mcp_servers.retrieve(stdio_id)
- assert get_attr(stdio_retrieved, "command") == stdio_config["command"]
+ assert get_attr(stdio_retrieved, "command") == stdio_config["config"]["command"]
sse_retrieved = client.mcp_servers.retrieve(sse_id)
- assert get_attr(sse_retrieved, "server_url") == sse_config["server_url"]
+ assert get_attr(sse_retrieved, "server_url") == sse_config["config"]["server_url"]
http_retrieved = client.mcp_servers.retrieve(http_id)
- assert get_attr(http_retrieved, "server_url") == http_config["server_url"]
+ assert get_attr(http_retrieved, "server_url") == http_config["config"]["server_url"]
finally:
# Cleanup all servers
@@ -581,18 +598,28 @@ def test_partial_update_preserves_fields(client: Letta):
server_name = f"partial-update-{uuid.uuid4().hex[:8]}"
server_config = {
"server_name": server_name,
- "type": "stdio",
- "command": "node",
- "args": ["server.js", "--port", "3000"],
- "env": {"NODE_ENV": "production", "PORT": "3000", "DEBUG": "false"},
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": "node",
+ "args": ["server.js", "--port", "3000"],
+ "env": {"NODE_ENV": "production", "PORT": "3000", "DEBUG": "false"},
+ },
}
created_server = client.mcp_servers.create(**server_config)
server_id = get_attr(created_server, "id")
try:
- # Update only the server name
- update_request = {"server_name": "renamed-server"}
+ # Update only the server name - note that we still need to provide config with mcp_server_type
+ # but we can leave other fields as None/unset
+ update_request = {
+ "server_name": "renamed-server",
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": "node", # Keep same command
+ "args": ["server.js", "--port", "3000"], # Keep same args
+ },
+ }
updated_server = client.mcp_servers.update(server_id, **update_request)
@@ -620,7 +647,14 @@ def test_concurrent_server_operations(client: Letta):
# Update all servers
for i, server_id in enumerate(servers_created):
- update_request = {"server_name": f"updated-concurrent-{i}"}
+ update_request = {
+ "server_name": f"updated-concurrent-{i}",
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": "python",
+ "args": [f"server_{i}.py"],
+ },
+ }
updated_server = client.mcp_servers.update(server_id, **update_request)
assert get_attr(updated_server, "server_name") == f"updated-concurrent-{i}"
@@ -641,7 +675,7 @@ def test_full_server_lifecycle(client: Letta):
# 1. Create server
server_name = f"lifecycle-test-{uuid.uuid4().hex[:8]}"
server_config = create_stdio_server_request(server_name, command="npx", args=["-y", "@modelcontextprotocol/server-everything"])
- server_config["env"]["TEST"] = "true"
+ server_config["config"]["env"]["TEST"] = "true"
created_server = client.mcp_servers.create(**server_config)
server_id = get_attr(created_server, "id")
@@ -656,7 +690,15 @@ def test_full_server_lifecycle(client: Letta):
assert get_attr(retrieved_server, "server_name") == server_name
# 4. Update server
- update_request = {"server_name": "lifecycle-updated", "env": {"TEST": "false", "NEW_VAR": "value"}}
+ update_request = {
+ "server_name": "lifecycle-updated",
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": "npx",
+ "args": ["-y", "@modelcontextprotocol/server-everything"],
+ "env": {"TEST": "false", "NEW_VAR": "value"},
+ },
+ }
updated_server = client.mcp_servers.update(server_id, **update_request)
assert get_attr(updated_server, "server_name") == "lifecycle-updated"
@@ -817,9 +859,11 @@ def test_mcp_multiple_tools_in_sequence_with_agent(client: Letta):
server_name = f"test-multi-tools-{uuid.uuid4().hex[:8]}"
server_config = {
"server_name": server_name,
- "type": "stdio",
- "command": sys.executable,
- "args": [str(mcp_server_path)],
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": sys.executable,
+ "args": [str(mcp_server_path)],
+ },
}
# Register the MCP server
@@ -915,9 +959,11 @@ def test_mcp_complex_schema_tool_with_agent(client: Letta):
server_name = f"test-complex-schema-{uuid.uuid4().hex[:8]}"
server_config = {
"server_name": server_name,
- "type": "stdio",
- "command": sys.executable,
- "args": [str(mcp_server_path)],
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": sys.executable,
+ "args": [str(mcp_server_path)],
+ },
}
# Register the MCP server
@@ -1064,9 +1110,11 @@ def test_comprehensive_mcp_server_tool_listing(client: Letta):
server_name = f"test-comprehensive-{uuid.uuid4().hex[:8]}"
server_config = {
"server_name": server_name,
- "type": "stdio",
- "command": sys.executable,
- "args": [str(mcp_server_path)],
+ "config": {
+ "mcp_server_type": "stdio",
+ "command": sys.executable,
+ "args": [str(mcp_server_path)],
+ },
}
# Register the MCP server