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.

chore: bump 0.9.1 (#2734)

cthomasf793a1fd449d
diff --git a/alembic/versions/06fbbf65d4f1_support_for_project_id_for_blocks_and_.py b/alembic/versions/06fbbf65d4f1_support_for_project_id_for_blocks_and_.py
new file mode 100644
index 000000000..8dab61ac2
--- /dev/null
+++ b/alembic/versions/06fbbf65d4f1_support_for_project_id_for_blocks_and_.py
@@ -0,0 +1,71 @@
+"""support for project_id for blocks and groups
+
+Revision ID: 06fbbf65d4f1
+Revises: f55542f37641
+Create Date: 2025-07-21 15:07:32.133538
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "06fbbf65d4f1"
+down_revision: Union[str, None] = "f55542f37641"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.add_column("block", sa.Column("project_id", sa.String(), nullable=True))
+ op.add_column("groups", sa.Column("project_id", sa.String(), nullable=True))
+
+ # NOTE: running the backfill on alembic will result in locking with running application.
+ # This is okay if okay with downtime. Options also to do rolling migration or dynamic updates.
+
+ # Backfill project_id for blocks table
+ # Since all agents for a block have the same project_id, we can just grab the first one
+ # op.execute(
+ # text(
+ # """
+ # UPDATE block
+ # SET project_id = (
+ # SELECT a.project_id
+ # FROM blocks_agents ba
+ # JOIN agents a ON ba.agent_id = a.id
+ # WHERE ba.block_id = block.id
+ # AND a.project_id IS NOT NULL
+ # LIMIT 1
+ # )
+ # """
+ # )
+ # )
+
+ # Backfill project_id for groups table
+ # op.execute(
+ # text(
+ # """
+ # UPDATE groups
+ # SET project_id = (
+ # SELECT a.project_id
+ # FROM groups_agents ga
+ # JOIN agents a ON ga.agent_id = a.id
+ # WHERE ga.group_id = groups.id
+ # AND a.project_id IS NOT NULL
+ # LIMIT 1
+ # )
+ # """
+ # )
+ # )
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_column("groups", "project_id")
+ op.drop_column("block", "project_id")
+ # ### end Alembic commands ###
diff --git a/alembic/versions/f55542f37641_add_index_for_agent_tags_reversed_order.py b/alembic/versions/f55542f37641_add_index_for_agent_tags_reversed_order.py
new file mode 100644
index 000000000..5a0d13d22
--- /dev/null
+++ b/alembic/versions/f55542f37641_add_index_for_agent_tags_reversed_order.py
@@ -0,0 +1,37 @@
+"""add index for agent_tags reversed order
+
+Revision ID: f55542f37641
+Revises: ddecfe4902bc
+Create Date: 2025-07-24 18:00:30.773048
+
+"""
+
+from typing import Sequence, Union
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "f55542f37641"
+down_revision: Union[str, None] = "f5d26b0526e8"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # Note some issues at least with pg8000 with concurrent index creation
+ # with op.get_context().autocommit_block():
+ # op.create_index(
+ # op.f('ix_agent_tags_tag_agent_id'),
+ # "agents_tags",
+ # ['tag', 'agent_id'],
+ # unique=False,
+ # postgresql_concurrently=True,
+ # )
+ op.create_index("ix_agents_tags_tag_agent_id", "agents_tags", ["tag", "agent_id"], unique=False)
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_index("ix_agents_tags_tag_agent_id", table_name="agents_tags")
+ # ### end Alembic commands ###
diff --git a/alembic/versions/f5d26b0526e8_add_mcp_oauth.py b/alembic/versions/f5d26b0526e8_add_mcp_oauth.py
new file mode 100644
index 000000000..52c9f764e
--- /dev/null
+++ b/alembic/versions/f5d26b0526e8_add_mcp_oauth.py
@@ -0,0 +1,67 @@
+"""add_mcp_oauth
+
+Revision ID: f5d26b0526e8
+Revises: ddecfe4902bc
+Create Date: 2025-07-24 12:34:05.795355
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "f5d26b0526e8"
+down_revision: Union[str, None] = "ddecfe4902bc"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_table(
+ "mcp_oauth",
+ sa.Column("id", sa.String(), nullable=False),
+ sa.Column("state", sa.String(length=255), nullable=False),
+ sa.Column("server_id", sa.String(length=255), nullable=True),
+ sa.Column("server_url", sa.Text(), nullable=False),
+ sa.Column("server_name", sa.Text(), nullable=False),
+ sa.Column("authorization_url", sa.Text(), nullable=True),
+ sa.Column("authorization_code", sa.Text(), nullable=True),
+ sa.Column("access_token", sa.Text(), nullable=True),
+ sa.Column("refresh_token", sa.Text(), nullable=True),
+ sa.Column("token_type", sa.String(length=50), nullable=False),
+ sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
+ sa.Column("scope", sa.Text(), nullable=True),
+ sa.Column("client_id", sa.Text(), nullable=True),
+ sa.Column("client_secret", sa.Text(), nullable=True),
+ sa.Column("redirect_uri", sa.Text(), nullable=True),
+ sa.Column("status", sa.String(length=20), nullable=False),
+ sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
+ sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
+ sa.Column("is_deleted", sa.Boolean(), server_default=sa.text("FALSE"), nullable=False),
+ sa.Column("_created_by_id", sa.String(), nullable=True),
+ sa.Column("_last_updated_by_id", sa.String(), nullable=True),
+ sa.Column("organization_id", sa.String(), nullable=False),
+ sa.Column("user_id", sa.String(), nullable=False),
+ sa.ForeignKeyConstraint(
+ ["organization_id"],
+ ["organizations.id"],
+ ),
+ sa.ForeignKeyConstraint(["server_id"], ["mcp_server.id"], ondelete="CASCADE"),
+ sa.ForeignKeyConstraint(
+ ["user_id"],
+ ["users.id"],
+ ),
+ sa.PrimaryKeyConstraint("id"),
+ sa.UniqueConstraint("state"),
+ )
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_table("mcp_oauth")
+ # ### end Alembic commands ###
diff --git a/letta/__init__.py b/letta/__init__.py
index 0ec0749e5..ba17d517d 100644
--- a/letta/__init__.py
+++ b/letta/__init__.py
@@ -5,7 +5,7 @@ try:
__version__ = version("letta")
except PackageNotFoundError:
# Fallback for development installations
- __version__ = "0.9.0"
+ __version__ = "0.9.1"
if os.environ.get("LETTA_VERSION"):
__version__ = os.environ["LETTA_VERSION"]
diff --git a/letta/agents/base_agent.py b/letta/agents/base_agent.py
index 1e5401b28..325090ec1 100644
--- a/letta/agents/base_agent.py
+++ b/letta/agents/base_agent.py
@@ -122,7 +122,7 @@ class BaseAgent(ABC):
curr_dynamic_section = extract_dynamic_section(curr_system_message_text)
# generate just the memory string with current state for comparison
- curr_memory_str = agent_state.memory.compile(
+ curr_memory_str = await agent_state.memory.compile_async(
tool_usage_rules=tool_constraint_block, sources=agent_state.sources, max_files_open=agent_state.max_files_open
)
new_dynamic_section = extract_dynamic_section(curr_memory_str)
diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.py
index 3c7960bce..3f7faab79 100644
--- a/letta/agents/letta_agent.py
+++ b/letta/agents/letta_agent.py
@@ -359,6 +359,8 @@ class LettaAgent(BaseAgent):
# Update step if it needs to be updated
finally:
if settings.track_stop_reason:
+ if step_progression == StepProgression.FINISHED and should_continue:
+ continue
self.logger.info("Running final update. Step Progression: %s", step_progression)
try:
if step_progression < StepProgression.STEP_LOGGED:
@@ -586,6 +588,8 @@ class LettaAgent(BaseAgent):
# Update step if it needs to be updated
finally:
if settings.track_stop_reason:
+ if step_progression == StepProgression.FINISHED and should_continue:
+ continue
self.logger.info("Running final update. Step Progression: %s", step_progression)
try:
if step_progression < StepProgression.STEP_LOGGED:
@@ -897,6 +901,8 @@ class LettaAgent(BaseAgent):
# Update step if it needs to be updated
finally:
if settings.track_stop_reason:
+ if step_progression == StepProgression.FINISHED and should_continue:
+ continue
self.logger.info("Running final update. Step Progression: %s", step_progression)
try:
if step_progression < StepProgression.STEP_LOGGED:
diff --git a/letta/helpers/datetime_helpers.py b/letta/helpers/datetime_helpers.py
index 0633add36..07856495b 100644
--- a/letta/helpers/datetime_helpers.py
+++ b/letta/helpers/datetime_helpers.py
@@ -42,7 +42,7 @@ def get_local_time_timezone(timezone=DEFAULT_TIMEZONE):
return formatted_time
-def get_local_time(timezone=DEFAULT_TIMEZONE):
+def get_local_time(timezone: str | None = DEFAULT_TIMEZONE):
if timezone is not None:
time_str = get_local_time_timezone(timezone)
else:
diff --git a/letta/helpers/json_helpers.py b/letta/helpers/json_helpers.py
index 2075c58dd..6618f274f 100644
--- a/letta/helpers/json_helpers.py
+++ b/letta/helpers/json_helpers.py
@@ -7,7 +7,7 @@ def json_loads(data):
return json.loads(data, strict=False)
-def json_dumps(data, indent=2):
+def json_dumps(data, indent=2) -> str:
def safe_serializer(obj):
if isinstance(obj, datetime):
return obj.isoformat()
diff --git a/letta/orm/agent.py b/letta/orm/agent.py
index bc2879d8f..a0128f87a 100644
--- a/letta/orm/agent.py
+++ b/letta/orm/agent.py
@@ -10,7 +10,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
from letta.orm.block import Block
from letta.orm.custom_columns import EmbeddingConfigColumn, LLMConfigColumn, ResponseFormatColumn, ToolRulesColumn
from letta.orm.identity import Identity
-from letta.orm.mixins import OrganizationMixin
+from letta.orm.mixins import OrganizationMixin, ProjectMixin
from letta.orm.organization import Organization
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.agent import AgentState as PydanticAgentState
@@ -31,7 +31,7 @@ if TYPE_CHECKING:
from letta.orm.tool import Tool
-class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
+class Agent(SqlalchemyBase, OrganizationMixin, ProjectMixin, AsyncAttrs):
__tablename__ = "agents"
__pydantic_model__ = PydanticAgentState
__table_args__ = (Index("ix_agents_created_at", "created_at", "id"),)
@@ -67,7 +67,6 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
embedding_config: Mapped[Optional[EmbeddingConfig]] = mapped_column(
EmbeddingConfigColumn, doc="the embedding configuration object for this agent."
)
- project_id: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The id of the project the agent belongs to.")
template_id: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The id of the template the agent belongs to.")
base_template_id: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The base template id of the agent.")
diff --git a/letta/orm/agents_tags.py b/letta/orm/agents_tags.py
index 438910a34..d71770832 100644
--- a/letta/orm/agents_tags.py
+++ b/letta/orm/agents_tags.py
@@ -9,6 +9,7 @@ class AgentsTags(Base):
__table_args__ = (
UniqueConstraint("agent_id", "tag", name="unique_agent_tag"),
Index("ix_agents_tags_agent_id_tag", "agent_id", "tag"),
+ Index("ix_agents_tags_tag_agent_id", "tag", "agent_id"),
)
# # agent generates its own id
diff --git a/letta/orm/block.py b/letta/orm/block.py
index bbbb6dfc1..a31b5a875 100644
--- a/letta/orm/block.py
+++ b/letta/orm/block.py
@@ -6,7 +6,7 @@ from sqlalchemy.orm import Mapped, attributes, declared_attr, mapped_column, rel
from letta.constants import CORE_MEMORY_BLOCK_CHAR_LIMIT
from letta.orm.block_history import BlockHistory
from letta.orm.blocks_agents import BlocksAgents
-from letta.orm.mixins import OrganizationMixin
+from letta.orm.mixins import OrganizationMixin, ProjectMixin
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.block import Block as PydanticBlock
from letta.schemas.block import Human, Persona
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
from letta.orm.identity import Identity
-class Block(OrganizationMixin, SqlalchemyBase):
+class Block(OrganizationMixin, SqlalchemyBase, ProjectMixin):
"""Blocks are sections of the LLM context, representing a specific part of the total Memory"""
__tablename__ = "block"
diff --git a/letta/orm/group.py b/letta/orm/group.py
index 489e563f5..fe2cfe7e2 100644
--- a/letta/orm/group.py
+++ b/letta/orm/group.py
@@ -4,12 +4,12 @@ from typing import List, Optional
from sqlalchemy import JSON, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
-from letta.orm.mixins import OrganizationMixin
+from letta.orm.mixins import OrganizationMixin, ProjectMixin
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.group import Group as PydanticGroup
-class Group(SqlalchemyBase, OrganizationMixin):
+class Group(SqlalchemyBase, OrganizationMixin, ProjectMixin):
__tablename__ = "groups"
__pydantic_model__ = PydanticGroup
diff --git a/letta/orm/identity.py b/letta/orm/identity.py
index ac83e9528..0d4f13ca8 100644
--- a/letta/orm/identity.py
+++ b/letta/orm/identity.py
@@ -1,17 +1,17 @@
import uuid
-from typing import List, Optional
+from typing import List
from sqlalchemy import String, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.orm import Mapped, mapped_column, relationship
-from letta.orm.mixins import OrganizationMixin
+from letta.orm.mixins import OrganizationMixin, ProjectMixin
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.identity import Identity as PydanticIdentity
from letta.schemas.identity import IdentityProperty
-class Identity(SqlalchemyBase, OrganizationMixin):
+class Identity(SqlalchemyBase, OrganizationMixin, ProjectMixin):
"""Identity ORM class"""
__tablename__ = "identities"
@@ -32,7 +32,6 @@ class Identity(SqlalchemyBase, OrganizationMixin):
identifier_key: Mapped[str] = mapped_column(nullable=False, doc="External, user-generated identifier key of the identity.")
name: Mapped[str] = mapped_column(nullable=False, doc="The name of the identity.")
identity_type: Mapped[str] = mapped_column(nullable=False, doc="The type of the identity.")
- project_id: Mapped[Optional[str]] = mapped_column(nullable=True, doc="The project id of the identity.")
properties: Mapped[List["IdentityProperty"]] = mapped_column(
JSON, nullable=False, default=list, doc="List of properties associated with the identity"
)
diff --git a/letta/orm/mcp_oauth.py b/letta/orm/mcp_oauth.py
new file mode 100644
index 000000000..e34f685a5
--- /dev/null
+++ b/letta/orm/mcp_oauth.py
@@ -0,0 +1,62 @@
+import uuid
+from datetime import datetime
+from enum import Enum
+from typing import Optional
+
+from sqlalchemy import DateTime, ForeignKey, String, Text
+from sqlalchemy.orm import Mapped, mapped_column
+
+from letta.orm.mixins import OrganizationMixin, UserMixin
+from letta.orm.sqlalchemy_base import SqlalchemyBase
+
+
+class OAuthSessionStatus(str, Enum):
+ """OAuth session status enumeration."""
+
+ PENDING = "pending"
+ AUTHORIZED = "authorized"
+ ERROR = "error"
+
+
+class MCPOAuth(SqlalchemyBase, OrganizationMixin, UserMixin):
+ """OAuth session model for MCP server authentication."""
+
+ __tablename__ = "mcp_oauth"
+
+ # Override the id field to match database UUID generation
+ id: Mapped[str] = mapped_column(String, primary_key=True, default=lambda: f"{uuid.uuid4()}")
+
+ # Core session information
+ state: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, doc="OAuth state parameter")
+ server_id: Mapped[str] = mapped_column(String(255), ForeignKey("mcp_server.id", ondelete="CASCADE"), nullable=True, doc="MCP server ID")
+ server_url: Mapped[str] = mapped_column(Text, nullable=False, doc="MCP server URL")
+ server_name: Mapped[str] = mapped_column(Text, nullable=False, doc="MCP server display name")
+
+ # OAuth flow data
+ authorization_url: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth authorization URL")
+ authorization_code: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth authorization code")
+
+ # Token data
+ access_token: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth access token")
+ refresh_token: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth refresh token")
+ token_type: Mapped[str] = mapped_column(String(50), default="Bearer", doc="Token type")
+ expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True, doc="Token expiry time")
+ scope: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth scope")
+
+ # Client configuration
+ client_id: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth client ID")
+ client_secret: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth client secret")
+ redirect_uri: Mapped[Optional[str]] = mapped_column(Text, nullable=True, doc="OAuth redirect URI")
+
+ # Session state
+ status: Mapped[OAuthSessionStatus] = mapped_column(String(20), default=OAuthSessionStatus.PENDING, doc="Session status")
+
+ # Timestamps
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(), doc="Session creation time")
+ updated_at: Mapped[datetime] = mapped_column(
+ DateTime(timezone=True), default=lambda: datetime.now(), onupdate=lambda: datetime.now(), doc="Last update time"
+ )
+
+ # Relationships (if needed in the future)
+ # user: Mapped[Optional["User"]] = relationship("User", back_populates="oauth_sessions")
+ # organization: Mapped["Organization"] = relationship("Organization", back_populates="oauth_sessions")
diff --git a/letta/orm/step.py b/letta/orm/step.py
index 05da631c2..e35aa1352 100644
--- a/letta/orm/step.py
+++ b/letta/orm/step.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional
from sqlalchemy import JSON, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
+from letta.orm.mixins import ProjectMixin
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.letta_stop_reason import StopReasonType
from letta.schemas.step import Step as PydanticStep
@@ -13,7 +14,7 @@ if TYPE_CHECKING:
from letta.orm.provider import Provider
-class Step(SqlalchemyBase):
+class Step(SqlalchemyBase, ProjectMixin):
"""Tracks all metadata for agent step."""
__tablename__ = "steps"
@@ -53,9 +54,6 @@ class Step(SqlalchemyBase):
feedback: Mapped[Optional[str]] = mapped_column(
None, nullable=True, doc="The feedback for this step. Must be either 'positive' or 'negative'."
)
- project_id: Mapped[Optional[str]] = mapped_column(
- None, nullable=True, doc="The project that the agent that executed this step belongs to (cloud only)."
- )
# Relationships (foreign keys)
organization: Mapped[Optional["Organization"]] = relationship("Organization")
diff --git a/letta/schemas/agent_file.py b/letta/schemas/agent_file.py
index 26b45cb04..c0e4fa479 100644
--- a/letta/schemas/agent_file.py
+++ b/letta/schemas/agent_file.py
@@ -1,5 +1,5 @@
from datetime import datetime
-from typing import Dict, List, Optional
+from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
@@ -8,6 +8,7 @@ from letta.schemas.block import Block, CreateBlock
from letta.schemas.enums import MessageRole
from letta.schemas.file import FileAgent, FileAgentBase, FileMetadata, FileMetadataBase
from letta.schemas.group import GroupCreate
+from letta.schemas.mcp import MCPServer
from letta.schemas.message import Message, MessageCreate
from letta.schemas.source import Source, SourceCreate
from letta.schemas.tool import Tool
@@ -264,9 +265,34 @@ class ToolSchema(Tool):
return cls(**tool.model_dump())
-# class MCPServerSchema(RegisterMCPServer):
-# """MCP Server with human-readable ID for agent file"""
-# id: str = Field(..., description="Human-readable identifier for this MCP server in the file")
+class MCPServerSchema(BaseModel):
+ """MCP server schema for agent files with remapped ID."""
+
+ __id_prefix__ = "mcp_server"
+
+ id: str = Field(..., description="Human-readable MCP server ID")
+ server_type: str
+ server_name: str
+ server_url: Optional[str] = None
+ stdio_config: Optional[Dict[str, Any]] = None
+ metadata_: Optional[Dict[str, Any]] = None
+
+ @classmethod
+ def from_mcp_server(cls, mcp_server: MCPServer) -> "MCPServerSchema":
+ """Convert MCPServer to MCPServerSchema (excluding auth fields)."""
+ return cls(
+ id=mcp_server.id, # remapped by serialization manager
+ server_type=mcp_server.server_type,
+ server_name=mcp_server.server_name,
+ server_url=mcp_server.server_url,
+ # exclude token, custom_headers, and the env field in stdio_config that may contain authentication credentials
+ stdio_config=cls.strip_env_from_stdio_config(mcp_server.stdio_config.model_dump()) if mcp_server.stdio_config else None,
+ metadata_=mcp_server.metadata_,
+ )
+
+ def strip_env_from_stdio_config(stdio_config: Dict[str, Any]) -> Dict[str, Any]:
+ """Strip out the env field from the stdio config."""
+ return {k: v for k, v in stdio_config.items() if k != "env"}
class AgentFileSchema(BaseModel):
@@ -278,7 +304,7 @@ class AgentFileSchema(BaseModel):
files: List[FileSchema] = Field(..., description="List of files in this agent file")
sources: List[SourceSchema] = Field(..., description="List of sources in this agent file")
tools: List[ToolSchema] = Field(..., description="List of tools in this agent file")
- # mcp_servers: List[MCPServerSchema] = Field(..., description="List of MCP servers in this agent file")
+ mcp_servers: List[MCPServerSchema] = Field(..., description="List of MCP servers in this agent file")
metadata: Dict[str, str] = Field(
default_factory=dict, description="Metadata for this agent file, including revision_id and other export information."
)
diff --git a/letta/schemas/block.py b/letta/schemas/block.py
index ea30da771..056045759 100644
--- a/letta/schemas/block.py
+++ b/letta/schemas/block.py
@@ -19,6 +19,7 @@ class BaseBlock(LettaBase, validate_assignment=True):
value: str = Field(..., description="Value of the block.")
limit: int = Field(CORE_MEMORY_BLOCK_CHAR_LIMIT, description="Character limit of the block.")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
# template data (optional)
template_name: Optional[str] = Field(None, description="Name of the block if it is a template.", alias="name")
is_template: bool = Field(False, description="Whether the block is a template (e.g. saved human/persona options).")
@@ -112,6 +113,7 @@ class BlockUpdate(BaseBlock):
limit: Optional[int] = Field(None, description="Character limit of the block.")
value: Optional[str] = Field(None, description="Value of the block.")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
class Config:
extra = "ignore" # Ignores extra fields
@@ -124,6 +126,7 @@ class CreateBlock(BaseBlock):
limit: int = Field(CORE_MEMORY_BLOCK_CHAR_LIMIT, description="Character limit of the block.")
value: str = Field(..., description="Value of the block.")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
# block templates
is_template: bool = False
template_name: Optional[str] = Field(None, description="Name of the block if it is a template.", alias="name")
diff --git a/letta/schemas/enums.py b/letta/schemas/enums.py
index a76178f9c..fba439cb0 100644
--- a/letta/schemas/enums.py
+++ b/letta/schemas/enums.py
@@ -102,6 +102,10 @@ class FileProcessingStatus(str, Enum):
COMPLETED = "completed"
ERROR = "error"
+ def is_terminal_state(self) -> bool:
+ """Check if the processing status is in a terminal state (completed or error)."""
+ return self in (FileProcessingStatus.COMPLETED, FileProcessingStatus.ERROR)
+
class ToolType(str, Enum):
CUSTOM = "custom"
diff --git a/letta/schemas/group.py b/letta/schemas/group.py
index de40ba5d0..fdbfff6d5 100644
--- a/letta/schemas/group.py
+++ b/letta/schemas/group.py
@@ -24,6 +24,7 @@ class Group(GroupBase):
manager_type: ManagerType = Field(..., description="")
agent_ids: List[str] = Field(..., description="")
description: str = Field(..., description="")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
shared_block_ids: List[str] = Field([], description="")
# Pattern fields
manager_agent_id: Optional[str] = Field(None, description="")
@@ -138,6 +139,7 @@ class GroupCreate(BaseModel):
agent_ids: List[str] = Field(..., description="")
description: str = Field(..., description="")
manager_config: ManagerConfigUnion = Field(RoundRobinManager(), description="")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
shared_block_ids: List[str] = Field([], description="")
@@ -145,4 +147,5 @@ class GroupUpdate(BaseModel):
agent_ids: Optional[List[str]] = Field(None, description="")
description: Optional[str] = Field(None, description="")
manager_config: Optional[ManagerConfigUpdateUnion] = Field(None, description="")
+ project_id: Optional[str] = Field(None, description="The associated project id.")
shared_block_ids: Optional[List[str]] = Field(None, description="")
diff --git a/letta/schemas/mcp.py b/letta/schemas/mcp.py
index b851f2d5d..f7070e8b0 100644
--- a/letta/schemas/mcp.py
+++ b/letta/schemas/mcp.py
@@ -1,3 +1,4 @@
+from datetime import datetime
from typing import Any, Dict, Optional, Union
from pydantic import Field
@@ -10,6 +11,7 @@ from letta.functions.mcp_client.types import (
StdioServerConfig,
StreamableHTTPServerConfig,
)
+from letta.orm.mcp_oauth import OAuthSessionStatus
from letta.schemas.letta_base import LettaBase
@@ -119,3 +121,71 @@ class UpdateStreamableHTTPMCPServer(LettaBase):
UpdateMCPServer = Union[UpdateSSEMCPServer, UpdateStdioMCPServer, UpdateStreamableHTTPMCPServer]
RegisterMCPServer = Union[RegisterSSEMCPServer, RegisterStdioMCPServer, RegisterStreamableHTTPMCPServer]
+
+
+# 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")
+
+ # 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")
+
+ # 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")
+
+ # 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")
+
+
+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")
diff --git a/letta/schemas/memory.py b/letta/schemas/memory.py
index c240bded0..e59a6437f 100644
--- a/letta/schemas/memory.py
+++ b/letta/schemas/memory.py
@@ -133,6 +133,25 @@ class Memory(BaseModel, validate_assignment=True):
except Exception as e:
raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")
+ async def set_prompt_template_async(self, prompt_template: str):
+ """
+ Async version of set_prompt_template that doesn't block the event loop.
+ """
+ try:
+ # Validate Jinja2 syntax with async enabled
+ Template(prompt_template, enable_async=True)
+
+ # Validate compatibility with current memory structure - use async rendering
+ template = Template(prompt_template, enable_async=True)
+ await template.render_async(blocks=self.blocks, file_blocks=self.file_blocks, sources=[], max_files_open=None)
+
+ # If we get here, the template is valid and compatible
+ self.prompt_template = prompt_template
+ except TemplateSyntaxError as e:
+ raise ValueError(f"Invalid Jinja2 template syntax: {str(e)}")
+ except Exception as e:
+ raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")
+
def compile(self, tool_usage_rules=None, sources=None, max_files_open=None) -> str:
"""Generate a string representation of the memory in-context using the Jinja2 template"""
try:
@@ -149,6 +168,22 @@ class Memory(BaseModel, validate_assignment=True):
except Exception as e:
raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")
+ async def compile_async(self, tool_usage_rules=None, sources=None, max_files_open=None) -> str:
+ """Async version of compile that doesn't block the event loop"""
+ try:
+ template = Template(self.prompt_template, enable_async=True)
+ return await template.render_async(
+ blocks=self.blocks,
+ file_blocks=self.file_blocks,
+ tool_usage_rules=tool_usage_rules,
+ sources=sources,
+ max_files_open=max_files_open,
+ )
+ except TemplateSyntaxError as e:
+ raise ValueError(f"Invalid Jinja2 template syntax: {str(e)}")
+ except Exception as e:
+ raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")
+
def list_block_labels(self) -> List[str]:
"""Return a list of the block names held inside the memory object"""
# return list(self.memory.keys())
diff --git a/letta/schemas/message.py b/letta/schemas/message.py
index 73bf73db6..8c15bead4 100644
--- a/letta/schemas/message.py
+++ b/letta/schemas/message.py
@@ -41,7 +41,7 @@ from letta.schemas.letta_message_content import (
get_letta_message_content_union_str_json_schema,
)
from letta.system import unpack_message
-from letta.utils import parse_json
+from letta.utils import parse_json, validate_function_response
def add_inner_thoughts_to_tool_call(
@@ -251,10 +251,10 @@ class Message(BaseMessage):
include_err: Optional[bool] = None,
) -> List[LettaMessage]:
"""Convert message object (in DB format) to the style used by the original Letta API"""
- messages = []
+ # TODO (cliandy): break this into more manageable pieces
if self.role == MessageRole.assistant:
-
+ messages = []
# Handle reasoning
if self.content:
# Check for ReACT-style COT inside of TextContent
@@ -348,7 +348,7 @@ class Message(BaseMessage):
# We need to unpack the actual message contents from the function call
try:
func_args = parse_json(tool_call.function.arguments)
- message_string = func_args[assistant_message_tool_kwarg]
+ message_string = validate_function_response(func_args[assistant_message_tool_kwarg], 0, truncate=False)
except KeyError:
raise ValueError(f"Function call {tool_call.function.name} missing {assistant_message_tool_kwarg} argument")
messages.append(
@@ -380,99 +380,106 @@ class Message(BaseMessage):
is_err=self.is_err,
)
)
- elif self.role == MessageRole.tool:
- # This is type ToolReturnMessage
- # Try to interpret the function return, recall that this is how we packaged:
- # def package_function_response(was_success, response_string, timestamp=None):
- # formatted_time = get_local_time() if timestamp is None else timestamp
- # packaged_message = {
- # "status": "OK" if was_success else "Failed",
- # "message": response_string,
- # "time": formatted_time,
- # }
- if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
- text_content = self.content[0].text
- else:
- raise ValueError(f"Invalid tool return (no text object on message): {self.content}")
- try:
- function_return = parse_json(text_content)
- text_content = str(function_return.get("message", text_content))
- status = function_return["status"]
- if status == "OK":
- status_enum = "success"
- elif status == "Failed":
- status_enum = "error"
- else:
- raise ValueError(f"Invalid status: {status}")
- except json.JSONDecodeError:
- raise ValueError(f"Failed to decode function return: {text_content}")
- assert self.tool_call_id is not None
- messages.append(
- # TODO make sure this is what the API returns
- # function_return may not match exactly...
- ToolReturnMessage(
- id=self.id,
- date=self.created_at,
- tool_return=text_content,
- status=self.tool_returns[0].status if self.tool_returns else status_enum,
- tool_call_id=self.tool_call_id,
- stdout=self.tool_returns[0].stdout if self.tool_returns else None,
- stderr=self.tool_returns[0].stderr if self.tool_returns else None,
- name=self.name,
- otid=Message.generate_otid_from_id(self.id, len(messages)),
- sender_id=self.sender_id,
- step_id=self.step_id,
- is_err=self.is_err,
- )
- )
+ elif self.role == MessageRole.tool:
+ messages = [self._convert_tool_message()]
elif self.role == MessageRole.user:
- # This is type UserMessage
- if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
- text_content = self.content[0].text
- elif self.content:
- text_content = self.content
- else:
- raise ValueError(f"Invalid user message (no text object on message): {self.content}")
-
- message = unpack_message(text_content)
- messages.append(
- UserMessage(
- id=self.id,
- date=self.created_at,
- content=message,
- name=self.name,
- otid=self.otid,
- sender_id=self.sender_id,
- step_id=self.step_id,
- is_err=self.is_err,
- )
- )
+ messages = [self._convert_user_message()]
elif self.role == MessageRole.system:
- # This is type SystemMessage
- if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
- text_content = self.content[0].text
- else:
- raise ValueError(f"Invalid system message (no text object on system): {self.content}")
-
- messages.append(
- SystemMessage(
- id=self.id,
- date=self.created_at,
- content=text_content,
- name=self.name,
- otid=self.otid,
- sender_id=self.sender_id,
- step_id=self.step_id,
- )
- )
+ messages = [self._convert_system_message()]
else:
- raise ValueError(self.role)
+ raise ValueError(f"Unknown role: {self.role}")
+
+ return messages[::-1] if reverse else messages
- if reverse:
- messages.reverse()
+ def _convert_tool_message(self) -> ToolReturnMessage:
+ """Convert tool role message to ToolReturnMessage
- return messages
+ the tool return is packaged as follows:
+ packaged_message = {
+ "status": "OK" if was_success else "Failed",
+ "message": response_string,
+ "time": formatted_time,
+ }
+ """
+ if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
+ text_content = self.content[0].text
+ else:
+ raise ValueError(f"Invalid tool return (no text object on message): {self.content}")
+
+ try:
+ function_return = parse_json(text_content)
+ message_text = str(function_return.get("message", text_content))
+ status = self._parse_tool_status(function_return["status"])
+ except json.JSONDecodeError:
+ raise ValueError(f"Failed to decode function return: {text_content}")
+
+ assert self.tool_call_id is not None
+
+ return ToolReturnMessage(
+ id=self.id,
+ date=self.created_at,
+ tool_return=message_text,
+ status=self.tool_returns[0].status if self.tool_returns else status,
+ tool_call_id=self.tool_call_id,
+ stdout=self.tool_returns[0].stdout if self.tool_returns else None,
+ stderr=self.tool_returns[0].stderr if self.tool_returns else None,
+ name=self.name,
+ otid=Message.generate_otid_from_id(self.id, 0),
+ sender_id=self.sender_id,
+ step_id=self.step_id,
+ is_err=self.is_err,
+ )
+
+ @staticmethod
+ def _parse_tool_status(status: str) -> Literal["success", "error"]:
+ """Convert tool status string to enum value"""
+ if status == "OK":
+ return "success"
+ elif status == "Failed":
+ return "error"
+ else:
+ raise ValueError(f"Invalid status: {status}")
+
+ def _convert_user_message(self) -> UserMessage:
+ """Convert user role message to UserMessage"""
+ # Extract text content
+ if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
+ text_content = self.content[0].text
+ elif self.content:
+ text_content = self.content
+ else:
+ raise ValueError(f"Invalid user message (no text object on message): {self.content}")
+
+ message = unpack_message(text_content)
+
+ return UserMessage(
+ id=self.id,
+ date=self.created_at,
+ content=message,
+ name=self.name,
+ otid=self.otid,
+ sender_id=self.sender_id,
+ step_id=self.step_id,
+ is_err=self.is_err,
+ )
+
+ def _convert_system_message(self) -> SystemMessage:
+ """Convert system role message to SystemMessage"""
+ if self.content and len(self.content) == 1 and isinstance(self.content[0], TextContent):
+ text_content = self.content[0].text
+ else:
+ raise ValueError(f"Invalid system message (no text object on system): {self.content}")
+
+ return SystemMessage(
+ id=self.id,
+ date=self.created_at,
+ content=text_content,
+ name=self.name,
+ otid=self.otid,
+ sender_id=self.sender_id,
+ step_id=self.step_id,
+ )
@staticmethod
def dict_to_message(
diff --git a/letta/schemas/providers/openai.py b/letta/schemas/providers/openai.py
index 2a3bc0b89..e1cb633e2 100644
--- a/letta/schemas/providers/openai.py
+++ b/letta/schemas/providers/openai.py
@@ -202,7 +202,7 @@ class OpenAIProvider(Provider):
if model_type not in ["text->embedding"]:
continue
else:
- logger.info(
+ logger.debug(
f"Skipping embedding models for %s by default, as we don't assume embeddings are supported."
"Please open an issue on GitHub if support is required.",
self.base_url,
diff --git a/letta/server/rest_api/app.py b/letta/server/rest_api/app.py
index 208613ecc..c12b722b0 100644
--- a/letta/server/rest_api/app.py
+++ b/letta/server/rest_api/app.py
@@ -12,7 +12,6 @@ from typing import Optional
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
-from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.cors import CORSMiddleware
from letta.__init__ import __version__ as letta_version
@@ -35,6 +34,7 @@ from letta.server.db import db_registry
# NOTE(charles): these are extra routes that are not part of v1 but we still need to mount to pass tests
from letta.server.rest_api.auth.index import setup_auth_router # TODO: probably remove right?
from letta.server.rest_api.interface import StreamingServerInterface
+from letta.server.rest_api.middleware import CheckPasswordMiddleware, ProfilerContextMiddleware
from letta.server.rest_api.routers.openai.chat_completions.chat_completions import router as openai_chat_completions_router
from letta.server.rest_api.routers.v1 import ROUTERS as v1_routes
from letta.server.rest_api.routers.v1.organizations import router as organizations_router
@@ -42,7 +42,7 @@ from letta.server.rest_api.routers.v1.users import router as users_router # TOD
from letta.server.rest_api.static_files import mount_static_files
from letta.server.rest_api.utils import SENTRY_ENABLED
from letta.server.server import SyncServer
-from letta.settings import settings
+from letta.settings import settings, telemetry_settings
if SENTRY_ENABLED:
import sentry_sdk
@@ -92,24 +92,6 @@ def generate_password():
random_password = os.getenv("LETTA_SERVER_PASSWORD") or generate_password()
-class CheckPasswordMiddleware(BaseHTTPMiddleware):
- async def dispatch(self, request, call_next):
- # Exclude health check endpoint from password protection
- if request.url.path in {"/v1/health", "/v1/health/", "/latest/health/"}:
- return await call_next(request)
-
- if (
- request.headers.get("X-BARE-PASSWORD") == f"password {random_password}"
- or request.headers.get("Authorization") == f"Bearer {random_password}"
- ):
- return await call_next(request)
-
- return JSONResponse(
- content={"detail": "Unauthorized"},
- status_code=401,
- )
-
-
@asynccontextmanager
async def lifespan(app_: FastAPI):
"""
@@ -117,6 +99,19 @@ async def lifespan(app_: FastAPI):
"""
worker_id = os.getpid()
+ if telemetry_settings.profiler:
+ try:
+ import googlecloudprofiler
+
+ googlecloudprofiler.start(
+ service="memgpt-server",
+ service_version=str(letta_version),
+ verbose=3,
+ )
+ logger.info("Profiler started.")
+ except Exception as exc:
+ logger.info("Profiler not enabled: %", exc)
+
logger.info(f"[Worker {worker_id}] Starting lifespan initialization")
logger.info(f"[Worker {worker_id}] Initializing database connections")
db_registry.initialize_sync()
@@ -283,11 +278,14 @@ def create_application() -> "FastAPI":
if (os.getenv("LETTA_SERVER_SECURE") == "true") or "--secure" in sys.argv:
print(f"▶ Using secure mode with password: {random_password}")
- app.add_middleware(CheckPasswordMiddleware)
+ app.add_middleware(CheckPasswordMiddleware, password=random_password)
# Add reverse proxy middleware to handle X-Forwarded-* headers
# app.add_middleware(ReverseProxyMiddleware, base_path=settings.server_base_path)
+ if telemetry_settings.profiler:
+ app.add_middleware(ProfilerContextMiddleware)
+
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
diff --git a/letta/server/rest_api/middleware/__init__.py b/letta/server/rest_api/middleware/__init__.py
new file mode 100644
index 000000000..223442c0b
--- /dev/null
+++ b/letta/server/rest_api/middleware/__init__.py
@@ -0,0 +1,4 @@
+from letta.server.rest_api.middleware.check_password import CheckPasswordMiddleware
+from letta.server.rest_api.middleware.profiler_context import ProfilerContextMiddleware
+
+__all__ = ["CheckPasswordMiddleware", "ProfilerContextMiddleware"]
diff --git a/letta/server/rest_api/middleware/check_password.py b/letta/server/rest_api/middleware/check_password.py
new file mode 100644
index 000000000..f1217e94e
--- /dev/null
+++ b/letta/server/rest_api/middleware/check_password.py
@@ -0,0 +1,24 @@
+from starlette.middleware.base import BaseHTTPMiddleware
+from starlette.responses import JSONResponse
+
+
+class CheckPasswordMiddleware(BaseHTTPMiddleware):
+ def __init__(self, app, password: str):
+ super().__init__(app)
+ self.password = password
+
+ async def dispatch(self, request, call_next):
+ # Exclude health check endpoint from password protection
+ if request.url.path in {"/v1/health", "/v1/health/", "/latest/health/"}:
+ return await call_next(request)
+
+ if (
+ request.headers.get("X-BARE-PASSWORD") == f"password {self.password}"
+ or request.headers.get("Authorization") == f"Bearer {self.password}"
+ ):
+ return await call_next(request)
+
+ return JSONResponse(
+ content={"detail": "Unauthorized"},
+ status_code=401,
+ )
diff --git a/letta/server/rest_api/middleware/profiler_context.py b/letta/server/rest_api/middleware/profiler_context.py
new file mode 100644
index 000000000..7af66865a
--- /dev/null
+++ b/letta/server/rest_api/middleware/profiler_context.py
@@ -0,0 +1,25 @@
+from starlette.middleware.base import BaseHTTPMiddleware
+
+
+class ProfilerContextMiddleware(BaseHTTPMiddleware):
+ """Middleware to set context if using profiler. Currently just uses google-cloud-profiler."""
+
+ async def dispatch(self, request, call_next):
+ ctx = None
+ if request.url.path in {"/v1/health", "/v1/health/"}:
+ return await call_next(request)
+ try:
+ labels = {
+ "method": request.method,
+ "path": request.url.path,
+ "endpoint": request.url.path,
+ }
+ import googlecloudprofiler
+
+ ctx = googlecloudprofiler.context.set_labels(**labels)
+ except:
+ return await call_next(request)
+ if ctx:
+ with ctx:
+ return await call_next(request)
+ return await call_next(request)
diff --git a/letta/server/rest_api/routers/v1/blocks.py b/letta/server/rest_api/routers/v1/blocks.py
index 2ccdc6f54..0320b8321 100644
--- a/letta/server/rest_api/routers/v1/blocks.py
+++ b/letta/server/rest_api/routers/v1/blocks.py
@@ -22,6 +22,7 @@ async def list_blocks(
name: Optional[str] = Query(None, description="Name of the block"),
identity_id: Optional[str] = Query(None, description="Search agents by identifier id"),
identifier_keys: Optional[List[str]] = Query(None, description="Search agents by identifier keys"),
+ project_id: Optional[str] = Query(None, description="Search blocks by project id"),
limit: Optional[int] = Query(50, description="Number of blocks to return"),
server: SyncServer = Depends(get_letta_server),
actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
@@ -34,6 +35,7 @@ async def list_blocks(
template_name=name,
identity_id=identity_id,
identifier_keys=identifier_keys,
+ project_id=project_id,
limit=limit,
)
diff --git a/letta/server/rest_api/routers/v1/groups.py b/letta/server/rest_api/routers/v1/groups.py
index bbb511574..14f95115c 100644
--- a/letta/server/rest_api/routers/v1/groups.py
+++ b/letta/server/rest_api/routers/v1/groups.py
@@ -31,12 +31,12 @@ def list_groups(
"""
actor = server.user_manager.get_user_or_default(user_id=actor_id)
return server.group_manager.list_groups(
+ actor=actor,
project_id=project_id,
manager_type=manager_type,
before=before,
after=after,
limit=limit,
- actor=actor,
)
diff --git a/letta/server/rest_api/routers/v1/sources.py b/letta/server/rest_api/routers/v1/sources.py
index efba90c30..992eb064e 100644
--- a/letta/server/rest_api/routers/v1/sources.py
+++ b/letta/server/rest_api/routers/v1/sources.py
@@ -2,6 +2,7 @@ import asyncio
import mimetypes
import os
import tempfile
+from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import List, Optional
@@ -393,6 +394,31 @@ async def get_file_metadata(
if file_metadata.source_id != source_id:
raise HTTPException(status_code=404, detail=f"File with id={file_id} not found in source {source_id}.")
+ # Check for timeout if status is not terminal
+ if not file_metadata.processing_status.is_terminal_state():
+ if file_metadata.created_at:
+ # Handle timezone differences between PostgreSQL (timezone-aware) and SQLite (timezone-naive)
+ if settings.letta_pg_uri_no_default:
+ # PostgreSQL: both datetimes are timezone-aware
+ timeout_threshold = datetime.now(timezone.utc) - timedelta(minutes=settings.file_processing_timeout_minutes)
+ file_created_at = file_metadata.created_at
+ else:
+ # SQLite: both datetimes should be timezone-naive
+ timeout_threshold = datetime.utcnow() - timedelta(minutes=settings.file_processing_timeout_minutes)
+ file_created_at = file_metadata.created_at
+
+ if file_created_at < timeout_threshold:
+ # Move file to error status with timeout message
+ timeout_message = settings.file_processing_timeout_error_message.format(settings.file_processing_timeout_minutes)
+ try:
+ file_metadata = await server.file_manager.update_file_status(
+ file_id=file_metadata.id, actor=actor, processing_status=FileProcessingStatus.ERROR, error_message=timeout_message
+ )
+ except ValueError as e:
+ # state transition was blocked - log it but don't fail the request
+ logger.warning(f"Could not update file to timeout error state: {str(e)}")
+ # continue with existing file_metadata
+
if should_use_pinecone() and file_metadata.processing_status == FileProcessingStatus.EMBEDDING:
ids = await list_pinecone_index_for_files(file_id=file_id, actor=actor)
logger.info(
diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.py
index 7e840a8c2..86d50a373 100644
--- a/letta/server/rest_api/routers/v1/tools.py
+++ b/letta/server/rest_api/routers/v1/tools.py
@@ -1,4 +1,6 @@
+import asyncio
import json
+from collections.abc import AsyncGenerator
from typing import Any, Dict, List, Optional, Union
from composio.client import ComposioClientError, HTTPError, NoItemsFound
@@ -11,27 +13,38 @@ from composio.exceptions import (
EnumStringNotFound,
)
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query
+from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field
+from starlette.responses import StreamingResponse
from letta.errors import LettaToolCreateError
from letta.functions.functions import derive_openai_json_schema
from letta.functions.mcp_client.exceptions import MCPTimeoutError
-from letta.functions.mcp_client.types import MCPServerType, MCPTool, SSEServerConfig, StdioServerConfig, StreamableHTTPServerConfig
+from letta.functions.mcp_client.types import MCPTool, SSEServerConfig, StdioServerConfig, StreamableHTTPServerConfig
from letta.helpers.composio_helpers import get_composio_api_key
+from letta.helpers.decorators import deprecated
from letta.llm_api.llm_client import LLMClient
from letta.log import get_logger
from letta.orm.errors import UniqueConstraintViolationError
+from letta.orm.mcp_oauth import OAuthSessionStatus
from letta.schemas.enums import MessageRole
from letta.schemas.letta_message import ToolReturnMessage
from letta.schemas.letta_message_content import TextContent
-from letta.schemas.mcp import UpdateSSEMCPServer, UpdateStdioMCPServer, UpdateStreamableHTTPMCPServer
+from letta.schemas.mcp import MCPOAuthSessionCreate, UpdateSSEMCPServer, UpdateStdioMCPServer, UpdateStreamableHTTPMCPServer
from letta.schemas.message import Message
from letta.schemas.tool import Tool, ToolCreate, ToolRunFromSource, ToolUpdate
+from letta.server.rest_api.streaming_response import StreamingResponseWithStatusCode
from letta.server.rest_api.utils import get_letta_server
from letta.server.server import SyncServer
-from letta.services.mcp.sse_client import AsyncSSEMCPClient
+from letta.services.mcp.oauth_utils import (
+ MCPOAuthSession,
+ create_oauth_provider,
+ drill_down_exception,
+ get_oauth_success_html,
+ oauth_stream_event,
+)
from letta.services.mcp.stdio_client import AsyncStdioMCPClient
-from letta.services.mcp.streamable_http_client import AsyncStreamableHTTPMCPClient
+from letta.services.mcp.types import OauthStreamEvent
from letta.settings import tool_settings
router = APIRouter(prefix="/tools", tags=["tools"])
@@ -475,7 +488,11 @@ async def add_mcp_tool(
)
tool_create = ToolCreate.from_mcp(mcp_server_name=mcp_server_name, mcp_tool=mcp_tool)
- return await server.tool_manager.create_mcp_tool_async(tool_create=tool_create, mcp_server_name=mcp_server_name, actor=actor)
+ # For config-based servers, use the server name as ID since they don't have database IDs
+ mcp_server_id = mcp_server_name
+ return await server.tool_manager.create_mcp_tool_async(
+ tool_create=tool_create, mcp_server_name=mcp_server_name, mcp_server_id=mcp_server_id, actor=actor
+ )
else:
return await server.mcp_manager.add_tool_from_mcp_server(mcp_server_name=mcp_server_name, mcp_tool_name=mcp_tool_name, actor=actor)
@@ -608,35 +625,26 @@ async def delete_mcp_server_from_config(
return [server.to_config() for server in all_servers]
[email protected]("/mcp/servers/test", response_model=List[MCPTool], operation_id="test_mcp_server")
+@deprecated("Deprecated in favor of /mcp/servers/connect which handles OAuth flow via SSE stream")
[email protected]("/mcp/servers/test", operation_id="test_mcp_server")
async def test_mcp_server(
request: Union[StdioServerConfig, SSEServerConfig, StreamableHTTPServerConfig] = Body(...),
+ server: SyncServer = Depends(get_letta_server),
+ actor_id: Optional[str] = Header(None, alias="user_id"),
):
"""
Test connection to an MCP server without adding it.
- Returns the list of available tools if successful.
+ Returns the list of available tools if successful, or OAuth information if OAuth is required.
"""
client = None
try:
- # create a temporary MCP client based on the server type
- if request.type == MCPServerType.SSE:
- if not isinstance(request, SSEServerConfig):
- request = SSEServerConfig(**request.model_dump())
- client = AsyncSSEMCPClient(request)
- elif request.type == MCPServerType.STREAMABLE_HTTP:
- if not isinstance(request, StreamableHTTPServerConfig):
- request = StreamableHTTPServerConfig(**request.model_dump())
- client = AsyncStreamableHTTPMCPClient(request)
- elif request.type == MCPServerType.STDIO:
- if not isinstance(request, StdioServerConfig):
- request = StdioServerConfig(**request.model_dump())
- client = AsyncStdioMCPClient(request)
- else:
- raise ValueError(f"Invalid MCP server type: {request.type}")
+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)
+ client = await server.mcp_manager.get_mcp_client(request, actor)
await client.connect_to_server()
tools = await client.list_tools()
- return tools
+
+ return {"status": "success", "tools": tools}
except ConnectionError as e:
raise HTTPException(
status_code=400,
@@ -672,6 +680,160 @@ async def test_mcp_server(
logger.warning(f"Error during MCP client cleanup: {cleanup_error}")
+ "/mcp/servers/connect",
+ response_model=None,
+ responses={
+ 200: {
+ "description": "Successful response",
+ "content": {
+ "text/event-stream": {"description": "Server-Sent Events stream"},
+ },
+ }
+ },
+ operation_id="connect_mcp_server",
+)
+async def connect_mcp_server(
+ request: Union[StdioServerConfig, SSEServerConfig, StreamableHTTPServerConfig] = Body(...),
+ server: SyncServer = Depends(get_letta_server),
+ actor_id: Optional[str] = Header(None, alias="user_id"),
+) -> 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.
+ """
+
+ async def oauth_stream_generator(
+ request: Union[StdioServerConfig, SSEServerConfig, StreamableHTTPServerConfig],
+ ) -> AsyncGenerator[str, None]:
+ client = None
+ oauth_provider = None
+ temp_client = None
+ connect_task = None
+
+ 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=actor_id)
+
+ # Create MCP client with respective transport type
+ try:
+ client = await server.mcp_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(f"OAuth not supported for stdio")
+ yield oauth_stream_event(OauthStreamEvent.ERROR, message=f"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
+
+ # OAuth required, yield state to client to prepare to handle authorization URL
+ yield oauth_stream_event(OauthStreamEvent.OAUTH_REQUIRED, message="OAuth authentication required")
+
+ # Create OAuth session to persist the state of the OAuth flow
+ session_create = MCPOAuthSessionCreate(
+ server_url=request.server_url,
+ server_name=request.server_name,
+ user_id=actor.id,
+ organization_id=actor.organization_id,
+ )
+ oauth_session = await server.mcp_manager.create_oauth_session(session_create, actor)
+ session_id = oauth_session.id
+
+ # Create OAuth provider for the instance of the stream connection
+ # Note: Using the correct API path for the callback
+ # do not edit this this is the correct url
+ redirect_uri = f"http://localhost:8283/v1/tools/mcp/oauth/callback/{session_id}"
+ oauth_provider = await create_oauth_provider(session_id, request.server_url, redirect_uri, server.mcp_manager, actor)
+
+ # Get authorization URL by triggering OAuth flow
+ temp_client = None
+ try:
+ temp_client = await server.mcp_manager.get_mcp_client(request, actor, oauth_provider)
+
+ # Run connect_to_server in background to avoid blocking
+ # This will trigger the OAuth flow and the redirect_handler will save the authorization URL to database
+ connect_task = asyncio.create_task(temp_client.connect_to_server())
+
+ # Give the OAuth flow time to trigger and save the URL
+ await asyncio.sleep(1.0)
+
+ # Fetch the authorization URL from database and yield state to client to proceed with handling authorization URL
+ auth_session = await server.mcp_manager.get_oauth_session_by_id(session_id, actor)
+ if auth_session and auth_session.authorization_url:
+ yield oauth_stream_event(OauthStreamEvent.AUTHORIZATION_URL, url=auth_session.authorization_url, session_id=session_id)
+
+ except Exception as e:
+ logger.error(f"Error triggering OAuth flow: {e}")
+ yield oauth_stream_event(OauthStreamEvent.ERROR, message=f"Failed to trigger OAuth: {str(e)}")
+
+ # Clean up active resources
+ if connect_task and not connect_task.done():
+ connect_task.cancel()
+ try:
+ await connect_task
+ except asyncio.CancelledError:
+ pass
+ if temp_client:
+ try:
+ await temp_client.cleanup()
+ except Exception as cleanup_error:
+ logger.warning(f"Error during temp MCP client cleanup: {cleanup_error}")
+ return
+
+ # Wait for user authorization (with timeout), client should render loading state until user completes the flow and /mcp/oauth/callback/{session_id} is hit
+ yield oauth_stream_event(OauthStreamEvent.WAITING_FOR_AUTH, message="Waiting for user authorization...")
+
+ # Callback handler will poll for authorization code and state and update the OAuth session
+ await connect_task
+
+ tools = await temp_client.list_tools(serialize=True)
+
+ yield oauth_stream_event(OauthStreamEvent.SUCCESS, tools=tools)
+ 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 connect_task and not connect_task.done():
+ connect_task.cancel()
+ try:
+ await connect_task
+ except asyncio.CancelledError:
+ pass
+ if client:
+ try:
+ await client.cleanup()
+ except Exception as cleanup_error:
+ detailed_error = drill_down_exception(cleanup_error)
+ logger.warning(f"Error during MCP client cleanup: {detailed_error}")
+ if temp_client:
+ try:
+ await temp_client.cleanup()
+ except Exception as cleanup_error:
+ # TODO: @jnjpng fix async cancel scope issue
+ # detailed_error = drill_down_exception(cleanup_error)
+ logger.warning(f"Aysnc cleanup confict during temp MCP client cleanup: {cleanup_error}")
+
+ return StreamingResponseWithStatusCode(oauth_stream_generator(request), media_type="text/event-stream")
+
+
class CodeInput(BaseModel):
code: str = Field(..., description="Python source code to parse for JSON schema")
@@ -693,6 +855,45 @@ async def generate_json_schema(
raise HTTPException(status_code=400, detail=f"Failed to generate schema: {str(e)}")
+# TODO: @jnjpng need to route this through cloud API for production
[email protected]("/mcp/oauth/callback/{session_id}", operation_id="mcp_oauth_callback", response_class=HTMLResponse)
+async def mcp_oauth_callback(
+ session_id: str,
+ code: Optional[str] = Query(None, description="OAuth authorization code"),
+ state: Optional[str] = Query(None, description="OAuth state parameter"),
+ error: Optional[str] = Query(None, description="OAuth error"),
+ error_description: Optional[str] = Query(None, description="OAuth error description"),
+):
+ """
+ Handle OAuth callback for MCP server authentication.
+ """
+ try:
+ oauth_session = MCPOAuthSession(session_id)
+
+ if error:
+ error_msg = f"OAuth error: {error}"
+ if error_description:
+ error_msg += f" - {error_description}"
+ await oauth_session.update_session_status(OAuthSessionStatus.ERROR)
+ return {"status": "error", "message": error_msg}
+
+ if not code or not state:
+ await oauth_session.update_session_status(OAuthSessionStatus.ERROR)
+ return {"status": "error", "message": "Missing authorization code or state"}
+
+ # Store authorization code
+ success = await oauth_session.store_authorization_code(code, state)
+ if not success:
+ await oauth_session.update_session_status(OAuthSessionStatus.ERROR)
+ return {"status": "error", "message": "Invalid state parameter"}
+
+ return HTMLResponse(content=get_oauth_success_html(), status_code=200)
+
+ except Exception as e:
+ logger.error(f"OAuth callback error: {e}")
+ return {"status": "error", "message": f"OAuth callback failed: {str(e)}"}
+
+
class GenerateToolInput(BaseModel):
tool_name: str = Field(..., description="Name of the tool to generate code for")
prompt: str = Field(..., description="User prompt to generate code")
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py
index 6302f6453..fed81a5bc 100644
--- a/letta/services/agent_manager.py
+++ b/letta/services/agent_manager.py
@@ -15,6 +15,8 @@ from letta.constants import (
BASE_TOOLS,
BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
BASE_VOICE_SLEEPTIME_TOOLS,
+ DEFAULT_CORE_MEMORY_SOURCE_CHAR_LIMIT,
+ DEFAULT_MAX_FILES_OPEN,
DEFAULT_TIMEZONE,
DEPRECATED_LETTA_TOOLS,
FILES_TOOLS,
@@ -1644,7 +1646,7 @@ class AgentManager:
# note: we only update the system prompt if the core memory is changed
# this means that the archival/recall memory statistics may be someout out of date
- curr_memory_str = agent_state.memory.compile(
+ curr_memory_str = await agent_state.memory.compile_async(
sources=agent_state.sources,
tool_usage_rules=tool_rules_solver.compile_tool_rule_prompts(),
max_files_open=agent_state.max_files_open,
@@ -1834,14 +1836,12 @@ class AgentManager:
agent_state = await self.get_agent_by_id_async(agent_id=agent_id, actor=actor, include_relationships=["memory", "sources"])
system_message = await self.message_manager.get_message_by_id_async(message_id=agent_state.message_ids[0], actor=actor)
temp_tool_rules_solver = ToolRulesSolver(agent_state.tool_rules)
- if (
- new_memory.compile(
- sources=agent_state.sources,
- tool_usage_rules=temp_tool_rules_solver.compile_tool_rule_prompts(),
- max_files_open=agent_state.max_files_open,
- )
- not in system_message.content[0].text
- ):
+ new_memory_str = await new_memory.compile_async(
+ sources=agent_state.sources,
+ tool_usage_rules=temp_tool_rules_solver.compile_tool_rule_prompts(),
+ max_files_open=agent_state.max_files_open,
+ )
+ if new_memory_str not in system_message.content[0].text:
# update the blocks (LRW) in the DB
for label in agent_state.memory.list_block_labels():
updated_value = new_memory.get_block(label).value
@@ -3169,6 +3169,12 @@ class AgentManager:
if max_files is None:
max_files = default_max_files
+ # FINAL fallback: ensure neither is None (should never happen, but just in case)
+ if per_file_limit is None:
+ per_file_limit = DEFAULT_CORE_MEMORY_SOURCE_CHAR_LIMIT
+ if max_files is None:
+ max_files = DEFAULT_MAX_FILES_OPEN
+
return per_file_limit, max_files
@enforce_types
diff --git a/letta/services/agent_serialization_manager.py b/letta/services/agent_serialization_manager.py
index 632fd77b6..3cb1ca020 100644
--- a/letta/services/agent_serialization_manager.py
+++ b/letta/services/agent_serialization_manager.py
@@ -1,6 +1,7 @@
from datetime import datetime, timezone
from typing import Dict, List
+from letta.constants import MCP_TOOL_TAG_NAME_PREFIX
from letta.errors import AgentFileExportError, AgentFileImportError
from letta.helpers.pinecone_utils import should_use_pinecone
from letta.log import get_logger
@@ -13,6 +14,7 @@ from letta.schemas.agent_file import (
FileSchema,
GroupSchema,
ImportResult,
+ MCPServerSchema,
MessageSchema,
SourceSchema,
ToolSchema,
@@ -20,6 +22,7 @@ from letta.schemas.agent_file import (
from letta.schemas.block import Block
from letta.schemas.enums import FileProcessingStatus
from letta.schemas.file import FileMetadata
+from letta.schemas.mcp import MCPServer
from letta.schemas.message import Message
from letta.schemas.source import Source
from letta.schemas.tool import Tool
@@ -92,7 +95,7 @@ class AgentSerializationManager:
ToolSchema.__id_prefix__: 0,
MessageSchema.__id_prefix__: 0,
FileAgentSchema.__id_prefix__: 0,
- # MCPServerSchema.__id_prefix__: 0,
+ MCPServerSchema.__id_prefix__: 0,
}
def _reset_state(self):
@@ -258,6 +261,53 @@ class AgentSerializationManager:
file_schema.source_id = self._map_db_to_file_id(file_metadata.source_id, SourceSchema.__id_prefix__, allow_new=False)
return file_schema
+ async def _extract_unique_mcp_servers(self, tools: List, actor: User) -> List:
+ """Extract unique MCP servers from tools based on metadata, using server_id if available, otherwise falling back to server_name."""
+ mcp_server_ids = set()
+ mcp_server_names = set()
+ for tool in tools:
+ # Check if tool has MCP metadata
+ if tool.metadata_ and MCP_TOOL_TAG_NAME_PREFIX in tool.metadata_:
+ mcp_metadata = tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]
+ # TODO: @jnjpng clean this up once we fully migrate to server_id being the main identifier
+ if "server_id" in mcp_metadata:
+ mcp_server_ids.add(mcp_metadata["server_id"])
+ elif "server_name" in mcp_metadata:
+ mcp_server_names.add(mcp_metadata["server_name"])
+
+ # Fetch MCP servers by ID
+ mcp_servers = []
+ fetched_server_ids = set()
+ if mcp_server_ids:
+ try:
+ mcp_servers = await self.mcp_manager.get_mcp_servers_by_ids(list(mcp_server_ids), actor)
+ fetched_server_ids.update([mcp_server.id for mcp_server in mcp_servers])
+ except Exception as e:
+ logger.warning(f"Failed to fetch MCP servers by IDs {mcp_server_ids}: {e}")
+
+ # Fetch MCP servers by name if not already fetched by ID
+ if mcp_server_names:
+ for server_name in mcp_server_names:
+ try:
+ mcp_server = await self.mcp_manager.get_mcp_server(server_name, actor)
+ if mcp_server and mcp_server.id not in fetched_server_ids:
+ mcp_servers.append(mcp_server)
+ except Exception as e:
+ logger.warning(f"Failed to fetch MCP server by name {server_name}: {e}")
+
+ return mcp_servers
+
+ def _convert_mcp_server_to_schema(self, mcp_server: MCPServer) -> MCPServerSchema:
+ """Convert MCPServer to MCPServerSchema with ID remapping and auth scrubbing"""
+ try:
+ mcp_file_id = self._map_db_to_file_id(mcp_server.id, MCPServerSchema.__id_prefix__, allow_new=False)
+ mcp_schema = MCPServerSchema.from_mcp_server(mcp_server)
+ mcp_schema.id = mcp_file_id
+ return mcp_schema
+ except Exception as e:
+ logger.error(f"Failed to convert MCP server {mcp_server.id}: {e}")
+ raise
+
async def export(self, agent_ids: List[str], actor: User) -> AgentFileSchema:
"""
Export agents and their related entities to AgentFileSchema format.
@@ -289,6 +339,13 @@ class AgentSerializationManager:
tool_set = self._extract_unique_tools(agent_states)
block_set = self._extract_unique_blocks(agent_states)
+ # Extract MCP servers from tools BEFORE conversion (must be done before ID mapping)
+ mcp_server_set = await self._extract_unique_mcp_servers(tool_set, actor)
+
+ # Map MCP server IDs before converting schemas
+ for mcp_server in mcp_server_set:
+ self._map_db_to_file_id(mcp_server.id, MCPServerSchema.__id_prefix__)
+
# Extract sources and files from agent states BEFORE conversion (with caching)
source_set, file_set = await self._extract_unique_sources_and_files_from_agents(agent_states, actor, files_agents_cache)
@@ -301,6 +358,7 @@ class AgentSerializationManager:
block_schemas = [self._convert_block_to_schema(block) for block in block_set]
source_schemas = [self._convert_source_to_schema(source) for source in source_set]
file_schemas = [self._convert_file_to_schema(file_metadata) for file_metadata in file_set]
+ mcp_server_schemas = [self._convert_mcp_server_to_schema(mcp_server) for mcp_server in mcp_server_set]
logger.info(f"Exporting {len(agent_ids)} agents to agent file format")
@@ -312,7 +370,7 @@ class AgentSerializationManager:
files=file_schemas,
sources=source_schemas,
tools=tool_schemas,
- # mcp_servers=[], # TODO: Extract and convert MCP servers
+ mcp_servers=mcp_server_schemas,
metadata={"revision_id": await get_latest_alembic_revision()},
created_at=datetime.now(timezone.utc),
)
@@ -359,7 +417,20 @@ class AgentSerializationManager:
# in-memory cache for file metadata to avoid repeated db calls
file_metadata_cache = {} # Maps database file ID to FileMetadata
- # 1. Create tools first (no dependencies) - using bulk upsert for efficiency
+ # 1. Create MCP servers first (tools depend on them)
+ if schema.mcp_servers:
+ for mcp_server_schema in schema.mcp_servers:
+ server_data = mcp_server_schema.model_dump(exclude={"id"})
+ filtered_server_data = self._filter_dict_for_model(server_data, MCPServer)
+ create_schema = MCPServer(**filtered_server_data)
+
+ # Note: We don't have auth info from export, so the user will need to re-configure auth.
+ # TODO: @jnjpng store metadata about obfuscated metadata to surface to the user
+ created_mcp_server = await self.mcp_manager.create_or_update_mcp_server(create_schema, actor)
+ file_to_db_ids[mcp_server_schema.id] = created_mcp_server.id
+ imported_count += 1
+
+ # 2. Create tools (may depend on MCP servers) - using bulk upsert for efficiency
if schema.tools:
# convert tool schemas to pydantic tools
pydantic_tools = []
@@ -559,6 +630,7 @@ class AgentSerializationManager:
(schema.files, FileSchema.__id_prefix__),
(schema.sources, SourceSchema.__id_prefix__),
(schema.tools, ToolSchema.__id_prefix__),
+ (schema.mcp_servers, MCPServerSchema.__id_prefix__),
]
for entities, expected_prefix in entity_checks:
@@ -601,6 +673,7 @@ class AgentSerializationManager:
("files", schema.files),
("sources", schema.sources),
("tools", schema.tools),
+ ("mcp_servers", schema.mcp_servers),
]
for entity_type, entities in entity_collections:
@@ -705,3 +778,11 @@ class AgentSerializationManager:
raise AgentFileImportError(f"Schema validation failed: {'; '.join(errors)}")
logger.info("Schema validation passed")
+
+ def _filter_dict_for_model(self, data: dict, model_cls):
+ """Filter a dictionary to only include keys that are in the model fields"""
+ try:
+ allowed = model_cls.model_fields.keys() # Pydantic v2
+ except AttributeError:
+ allowed = model_cls.__fields__.keys() # Pydantic v1
+ return {k: v for k, v in data.items() if k in allowed}
diff --git a/letta/services/block_manager.py b/letta/services/block_manager.py
index d8682bd88..c0799026c 100644
--- a/letta/services/block_manager.py
+++ b/letta/services/block_manager.py
@@ -178,6 +178,7 @@ class BlockManager:
template_name: Optional[str] = None,
identity_id: Optional[str] = None,
identifier_keys: Optional[List[str]] = None,
+ project_id: Optional[str] = None,
before: Optional[str] = None,
after: Optional[str] = None,
limit: Optional[int] = 50,
@@ -210,6 +211,9 @@ class BlockManager:
if template_name:
query = query.where(BlockModel.template_name == template_name)
+ if project_id:
+ query = query.where(BlockModel.project_id == project_id)
+
needs_distinct = False
if identifier_keys:
diff --git a/letta/services/file_manager.py b/letta/services/file_manager.py
index b86d766c1..6dc067783 100644
--- a/letta/services/file_manager.py
+++ b/letta/services/file_manager.py
@@ -151,7 +151,8 @@ class FileManager:
Enforces state transition rules (when enforce_state_transitions=True):
- PENDING -> PARSING -> EMBEDDING -> COMPLETED (normal flow)
- Any non-terminal state -> ERROR
- - ERROR and COMPLETED are terminal (no transitions allowed)
+ - Same-state transitions are allowed (e.g., EMBEDDING -> EMBEDDING)
+ - ERROR and COMPLETED are terminal (no status transitions allowed, metadata updates blocked)
Args:
file_id: ID of the file to update
@@ -196,22 +197,31 @@ class FileManager:
]
# only add state transition validation if enforce_state_transitions is True
- if enforce_state_transitions:
- # prevent updates to terminal states (ERROR, COMPLETED)
+ if enforce_state_transitions and processing_status is not None:
+ # enforce specific transitions based on target status
+ if processing_status == FileProcessingStatus.PARSING:
+ where_conditions.append(
+ FileMetadataModel.processing_status.in_([FileProcessingStatus.PENDING, FileProcessingStatus.PARSING])
+ )
+ elif processing_status == FileProcessingStatus.EMBEDDING:
+ where_conditions.append(
+ FileMetadataModel.processing_status.in_([FileProcessingStatus.PARSING, FileProcessingStatus.EMBEDDING])
+ )
+ elif processing_status == FileProcessingStatus.COMPLETED:
+ where_conditions.append(
+ FileMetadataModel.processing_status.in_([FileProcessingStatus.EMBEDDING, FileProcessingStatus.COMPLETED])
+ )
+ elif processing_status == FileProcessingStatus.ERROR:
+ # ERROR can be set from any non-terminal state
+ where_conditions.append(
+ FileMetadataModel.processing_status.notin_([FileProcessingStatus.ERROR, FileProcessingStatus.COMPLETED])
+ )
+ elif enforce_state_transitions and processing_status is None:
+ # If only updating metadata fields (not status), prevent updates to terminal states
where_conditions.append(
FileMetadataModel.processing_status.notin_([FileProcessingStatus.ERROR, FileProcessingStatus.COMPLETED])
)
- if processing_status is not None:
- # enforce specific transitions based on target status
- if processing_status == FileProcessingStatus.PARSING:
- where_conditions.append(FileMetadataModel.processing_status == FileProcessingStatus.PENDING)
- elif processing_status == FileProcessingStatus.EMBEDDING:
- where_conditions.append(FileMetadataModel.processing_status == FileProcessingStatus.PARSING)
- elif processing_status == FileProcessingStatus.COMPLETED:
- where_conditions.append(FileMetadataModel.processing_status == FileProcessingStatus.EMBEDDING)
- # ERROR can be set from any non-terminal state (already handled by terminal check above)
-
# fast in-place update with state validation
stmt = (
update(FileMetadataModel)
diff --git a/letta/services/file_processor/file_processor.py b/letta/services/file_processor/file_processor.py
index 92f6c0862..d27597a43 100644
--- a/letta/services/file_processor/file_processor.py
+++ b/letta/services/file_processor/file_processor.py
@@ -69,6 +69,15 @@ class FileProcessor:
raise ValueError("No chunks created from text")
all_chunks.extend(chunks)
+ # Update with chunks length
+ file_metadata = await self.file_manager.update_file_status(
+ file_id=file_metadata.id,
+ actor=self.actor,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ total_chunks=len(all_chunks),
+ chunks_embedded=0,
+ )
+
all_passages = await self.embedder.generate_embedded_passages(
file_id=file_metadata.id,
source_id=source_id,
@@ -177,9 +186,7 @@ class FileProcessor:
"file_processor.ocr_completed",
{"filename": filename, "pages_extracted": len(ocr_response.pages), "text_length": len(raw_markdown_text)},
)
- file_metadata = await self.file_manager.update_file_status(
- file_id=file_metadata.id, actor=self.actor, processing_status=FileProcessingStatus.EMBEDDING
- )
+
file_metadata = await self.file_manager.upsert_file_content(file_id=file_metadata.id, text=raw_markdown_text, actor=self.actor)
await self.agent_manager.insert_file_into_context_windows(
@@ -241,13 +248,7 @@ class FileProcessor:
file_id=file_metadata.id,
actor=self.actor,
processing_status=FileProcessingStatus.COMPLETED,
- )
- else:
- await self.file_manager.update_file_status(
- file_id=file_metadata.id,
- actor=self.actor,
- total_chunks=len(all_passages),
- chunks_embedded=0,
+ chunks_embedded=len(all_passages),
)
return all_passages
@@ -286,6 +287,7 @@ class FileProcessor:
document_annotation=None,
)
+ # TODO: The file state machine here is kind of out of date, we need to match with the correct one above
@trace_method
async def process_imported_file(self, file_metadata: FileMetadata, source_id: str) -> List[Passage]:
"""Process an imported file that already has content - skip OCR, do chunking/embedding"""
diff --git a/letta/services/mcp/base_client.py b/letta/services/mcp/base_client.py
index 8aeda67f1..0df3cba12 100644
--- a/letta/services/mcp/base_client.py
+++ b/letta/services/mcp/base_client.py
@@ -1,9 +1,9 @@
-import asyncio
from contextlib import AsyncExitStack
from typing import Optional, Tuple
from mcp import ClientSession
from mcp import Tool as MCPTool
+from mcp.client.auth import OAuthClientProvider
from mcp.types import TextContent
from letta.functions.mcp_client.types import BaseServerConfig
@@ -14,14 +14,12 @@ logger = get_logger(__name__)
# TODO: Get rid of Async prefix on this class name once we deprecate old sync code
class AsyncBaseMCPClient:
- def __init__(self, server_config: BaseServerConfig):
+ def __init__(self, server_config: BaseServerConfig, oauth_provider: Optional[OAuthClientProvider] = None):
self.server_config = server_config
+ self.oauth_provider = oauth_provider
self.exit_stack = AsyncExitStack()
self.session: Optional[ClientSession] = None
self.initialized = False
- # Track the task that created this client
- self._creation_task = asyncio.current_task()
- self._cleanup_queue = asyncio.Queue(maxsize=1)
async def connect_to_server(self):
try:
@@ -48,9 +46,25 @@ class AsyncBaseMCPClient:
async def _initialize_connection(self, server_config: BaseServerConfig) -> None:
raise NotImplementedError("Subclasses must implement _initialize_connection")
- async def list_tools(self) -> list[MCPTool]:
+ async def list_tools(self, serialize: bool = False) -> list[MCPTool]:
self._check_initialized()
response = await self.session.list_tools()
+ if serialize:
+ serializable_tools = []
+ for tool in response.tools:
+ if hasattr(tool, "model_dump"):
+ # Pydantic model - use model_dump
+ serializable_tools.append(tool.model_dump())
+ elif hasattr(tool, "dict"):
+ # Older Pydantic model - use dict()
+ serializable_tools.append(tool.dict())
+ elif hasattr(tool, "__dict__"):
+ # Regular object - use __dict__
+ serializable_tools.append(tool.__dict__)
+ else:
+ # Fallback - convert to string
+ serializable_tools.append(str(tool))
+ return serializable_tools
return response.tools
async def execute_tool(self, tool_name: str, tool_args: dict) -> Tuple[str, bool]:
@@ -79,29 +93,7 @@ class AsyncBaseMCPClient:
# TODO: still hitting some async errors for voice agents, need to fix
async def cleanup(self):
- """Clean up resources - ensure this runs in the same task"""
- if hasattr(self, "_cleanup_task"):
- # If we're in a different task, schedule cleanup in original task
- current_task = asyncio.current_task()
- if current_task != self._creation_task:
- # Create a future to signal completion
- cleanup_done = asyncio.Future()
- self._cleanup_queue.put_nowait((self.exit_stack, cleanup_done))
- await cleanup_done
- return
-
- # Normal cleanup
await self.exit_stack.aclose()
def to_sync_client(self):
raise NotImplementedError("Subclasses must implement to_sync_client")
-
- async def __aenter__(self):
- """Enter the async context manager."""
- await self.connect_to_server()
- return self
-
- async def __aexit__(self, exc_type, exc_val, exc_tb):
- """Exit the async context manager."""
- await self.cleanup()
- return False # Don't suppress exceptions
diff --git a/letta/services/mcp/oauth_utils.py b/letta/services/mcp/oauth_utils.py
new file mode 100644
index 000000000..7391474ce
--- /dev/null
+++ b/letta/services/mcp/oauth_utils.py
@@ -0,0 +1,433 @@
+"""OAuth utilities for MCP server authentication."""
+
+import asyncio
+import json
+import secrets
+import time
+import uuid
+from datetime import datetime, timedelta
+from typing import Callable, Optional, Tuple
+
+from mcp.client.auth import OAuthClientProvider, TokenStorage
+from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
+from sqlalchemy import select
+
+from letta.log import get_logger
+from letta.orm.mcp_oauth import MCPOAuth, OAuthSessionStatus
+from letta.schemas.mcp import MCPOAuthSessionUpdate
+from letta.schemas.user import User as PydanticUser
+from letta.server.db import db_registry
+from letta.services.mcp.types import OauthStreamEvent
+from letta.services.mcp_manager import MCPManager
+
+logger = get_logger(__name__)
+
+
+class DatabaseTokenStorage(TokenStorage):
+ """Database-backed token storage using MCPOAuth table via mcp_manager."""
+
+ def __init__(self, session_id: str, mcp_manager: MCPManager, actor: PydanticUser):
+ self.session_id = session_id
+ self.mcp_manager = mcp_manager
+ self.actor = actor
+
+ async def get_tokens(self) -> Optional[OAuthToken]:
+ """Retrieve tokens from database."""
+ oauth_session = await self.mcp_manager.get_oauth_session_by_id(self.session_id, self.actor)
+ if not oauth_session or not oauth_session.access_token:
+ return None
+
+ return OAuthToken(
+ access_token=oauth_session.access_token,
+ refresh_token=oauth_session.refresh_token,
+ token_type=oauth_session.token_type,
+ expires_in=int(oauth_session.expires_at.timestamp() - time.time()),
+ scope=oauth_session.scope,
+ )
+
+ async def set_tokens(self, tokens: OAuthToken) -> None:
+ """Store tokens in database."""
+ session_update = MCPOAuthSessionUpdate(
+ access_token=tokens.access_token,
+ refresh_token=tokens.refresh_token,
+ token_type=tokens.token_type,
+ expires_at=datetime.fromtimestamp(tokens.expires_in + time.time()),
+ scope=tokens.scope,
+ status=OAuthSessionStatus.AUTHORIZED,
+ )
+ await self.mcp_manager.update_oauth_session(self.session_id, session_update, self.actor)
+
+ async def get_client_info(self) -> Optional[OAuthClientInformationFull]:
+ """Retrieve client information from database."""
+ oauth_session = await self.mcp_manager.get_oauth_session_by_id(self.session_id, self.actor)
+ if not oauth_session or not oauth_session.client_id:
+ return None
+
+ return OAuthClientInformationFull(
+ client_id=oauth_session.client_id,
+ client_secret=oauth_session.client_secret,
+ redirect_uris=[oauth_session.redirect_uri] if oauth_session.redirect_uri else [],
+ )
+
+ async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
+ """Store client information in database."""
+ session_update = MCPOAuthSessionUpdate(
+ client_id=client_info.client_id,
+ client_secret=client_info.client_secret,
+ redirect_uri=str(client_info.redirect_uris[0]) if client_info.redirect_uris else None,
+ )
+ await self.mcp_manager.update_oauth_session(self.session_id, session_update, self.actor)
+
+
+class MCPOAuthSession:
+ """Legacy OAuth session class - deprecated, use mcp_manager directly."""
+
+ def __init__(self, server_url: str, server_name: str, user_id: Optional[str], organization_id: str):
+ self.server_url = server_url
+ self.server_name = server_name
+ self.user_id = user_id
+ self.organization_id = organization_id
+ self.session_id = str(uuid.uuid4())
+ self.state = secrets.token_urlsafe(32)
+
+ def __init__(self, session_id: str):
+ self.session_id = session_id
+
+ # TODO: consolidate / deprecate this in favor of mcp_manager access
+ async def create_session(self) -> str:
+ """Create a new OAuth session in the database."""
+ async with db_registry.async_session() as session:
+ oauth_record = MCPOAuth(
+ id=self.session_id,
+ state=self.state,
+ server_url=self.server_url,
+ server_name=self.server_name,
+ user_id=self.user_id,
+ organization_id=self.organization_id,
+ status=OAuthSessionStatus.PENDING,
+ created_at=datetime.now(),
+ updated_at=datetime.now(),
+ )
+ oauth_record = await oauth_record.create_async(session, actor=None)
+
+ return self.session_id
+
+ async def get_session_status(self) -> OAuthSessionStatus:
+ """Get the current status of the OAuth session."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_record = await MCPOAuth.read_async(db_session=session, identifier=self.session_id, actor=None)
+ return oauth_record.status
+ except Exception:
+ return OAuthSessionStatus.ERROR
+
+ async def update_session_status(self, status: OAuthSessionStatus) -> None:
+ """Update the session status."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_record = await MCPOAuth.read_async(db_session=session, identifier=self.session_id, actor=None)
+ oauth_record.status = status
+ oauth_record.updated_at = datetime.now()
+ await oauth_record.update_async(db_session=session, actor=None)
+ except Exception:
+ pass
+
+ async def store_authorization_code(self, code: str, state: str) -> bool:
+ """Store the authorization code from OAuth callback."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_record = await MCPOAuth.read_async(db_session=session, identifier=self.session_id, actor=None)
+
+ # if oauth_record.state != state:
+ # return False
+
+ oauth_record.authorization_code = code
+ oauth_record.state = state
+ oauth_record.status = OAuthSessionStatus.AUTHORIZED
+ oauth_record.updated_at = datetime.now()
+ await oauth_record.update_async(db_session=session, actor=None)
+ return True
+ except Exception:
+ return False
+
+ async def get_authorization_url(self) -> Optional[str]:
+ """Get the authorization URL for this session."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_record = await MCPOAuth.read_async(db_session=session, identifier=self.session_id, actor=None)
+ return oauth_record.authorization_url
+ except Exception:
+ return None
+
+ async def set_authorization_url(self, url: str) -> None:
+ """Set the authorization URL for this session."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_record = await MCPOAuth.read_async(db_session=session, identifier=self.session_id, actor=None)
+ oauth_record.authorization_url = url
+ oauth_record.updated_at = datetime.now()
+ await oauth_record.update_async(db_session=session, actor=None)
+ except Exception:
+ pass
+
+
+async def create_oauth_provider(
+ session_id: str,
+ server_url: str,
+ redirect_uri: str,
+ mcp_manager: MCPManager,
+ actor: PydanticUser,
+ url_callback: Optional[Callable[[str], None]] = None,
+) -> OAuthClientProvider:
+ """Create an OAuth provider for MCP server authentication."""
+
+ client_metadata_dict = {
+ "client_name": "Letta MCP Client",
+ "redirect_uris": [redirect_uri],
+ "grant_types": ["authorization_code", "refresh_token"],
+ "response_types": ["code"],
+ "token_endpoint_auth_method": "client_secret_post",
+ }
+
+ # Use manager-based storage
+ storage = DatabaseTokenStorage(session_id, mcp_manager, actor)
+
+ # Extract base URL (remove /mcp endpoint if present)
+ oauth_server_url = server_url.rstrip("/").removesuffix("/sse").removesuffix("/mcp")
+
+ async def redirect_handler(authorization_url: str) -> None:
+ """Handle OAuth redirect by storing the authorization URL."""
+ logger.info(f"OAuth redirect handler called with URL: {authorization_url}")
+ session_update = MCPOAuthSessionUpdate(authorization_url=authorization_url)
+ await mcp_manager.update_oauth_session(session_id, session_update, actor)
+ logger.info(f"OAuth authorization URL stored: {authorization_url}")
+
+ # Call the callback if provided (e.g., to yield URL to SSE stream)
+ if url_callback:
+ url_callback(authorization_url)
+
+ async def callback_handler() -> Tuple[str, Optional[str]]:
+ """Handle OAuth callback by waiting for authorization code."""
+ timeout = 300 # 5 minutes
+ start_time = time.time()
+
+ logger.info(f"Waiting for authorization code for session {session_id}")
+ while time.time() - start_time < timeout:
+ oauth_session = await mcp_manager.get_oauth_session_by_id(session_id, actor)
+ if oauth_session and oauth_session.authorization_code:
+ return oauth_session.authorization_code, oauth_session.state
+ elif oauth_session and oauth_session.status == OAuthSessionStatus.ERROR:
+ raise Exception("OAuth authorization failed")
+ await asyncio.sleep(1)
+
+ raise Exception(f"Timeout waiting for OAuth callback after {timeout} seconds")
+
+ return OAuthClientProvider(
+ server_url=oauth_server_url,
+ client_metadata=OAuthClientMetadata.model_validate(client_metadata_dict),
+ storage=storage,
+ redirect_handler=redirect_handler,
+ callback_handler=callback_handler,
+ )
+
+
+async def cleanup_expired_oauth_sessions(max_age_hours: int = 24) -> None:
+ """Clean up expired OAuth sessions."""
+ cutoff_time = datetime.now() - timedelta(hours=max_age_hours)
+
+ async with db_registry.async_session() as session:
+ result = await session.execute(select(MCPOAuth).where(MCPOAuth.created_at < cutoff_time))
+ expired_sessions = result.scalars().all()
+
+ for oauth_session in expired_sessions:
+ await oauth_session.hard_delete_async(db_session=session, actor=None)
+
+ if expired_sessions:
+ logger.info(f"Cleaned up {len(expired_sessions)} expired OAuth sessions")
+
+
+def oauth_stream_event(event: OauthStreamEvent, **kwargs) -> str:
+ data = {"event": event.value}
+ data.update(kwargs)
+ return f"data: {json.dumps(data)}\n\n"
+
+
+def drill_down_exception(exception, depth=0, max_depth=5):
+ """Recursively drill down into nested exceptions to find the root cause"""
+ indent = " " * depth
+ error_details = []
+
+ error_details.append(f"{indent}Exception at depth {depth}:")
+ error_details.append(f"{indent} Type: {type(exception).__name__}")
+ error_details.append(f"{indent} Message: {str(exception)}")
+ error_details.append(f"{indent} Module: {getattr(type(exception), '__module__', 'unknown')}")
+
+ # Check for exception groups (TaskGroup errors)
+ if hasattr(exception, "exceptions") and exception.exceptions:
+ error_details.append(f"{indent} ExceptionGroup with {len(exception.exceptions)} sub-exceptions:")
+ for i, sub_exc in enumerate(exception.exceptions):
+ error_details.append(f"{indent} Sub-exception {i}:")
+ if depth < max_depth:
+ error_details.extend(drill_down_exception(sub_exc, depth + 1, max_depth))
+
+ # Check for chained exceptions (__cause__ and __context__)
+ if hasattr(exception, "__cause__") and exception.__cause__ and depth < max_depth:
+ error_details.append(f"{indent} Caused by:")
+ error_details.extend(drill_down_exception(exception.__cause__, depth + 1, max_depth))
+
+ if hasattr(exception, "__context__") and exception.__context__ and depth < max_depth:
+ error_details.append(f"{indent} Context:")
+ error_details.extend(drill_down_exception(exception.__context__, depth + 1, max_depth))
+
+ # Add traceback info
+ import traceback
+
+ if hasattr(exception, "__traceback__") and exception.__traceback__:
+ tb_lines = traceback.format_tb(exception.__traceback__)
+ error_details.append(f"{indent} Traceback:")
+ for line in tb_lines[-3:]: # Show last 3 traceback lines
+ error_details.append(f"{indent} {line.strip()}")
+
+ error_info = "".join(error_details)
+ return error_info
+
+
+def get_oauth_success_html() -> str:
+ """Generate HTML for successful OAuth authorization."""
+ return """
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Authorization Successful - Letta</title>
+ <style>
+ * {
+ margin: 0;
+ padding: 0;
+ box-sizing: border-box;
+ }
+
+ body {
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ min-height: 100vh;
+ margin: 0;
+ background-color: #f5f5f5;
+ background-image: url("data:image/svg+xml,%3Csvg width='1440' height='860' viewBox='0 0 1440 860' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cg clip-path='url(%23clip0_14823_146864)'%3E%3Cpath d='M720.001 1003.14C1080.62 1003.14 1372.96 824.028 1372.96 603.083C1372.96 382.138 1080.62 203.026 720.001 203.026C359.384 203.026 67.046 382.138 67.046 603.083C67.046 824.028 359.384 1003.14 720.001 1003.14Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 978.04C910.334 978.04 1064.63 883.505 1064.63 766.891C1064.63 650.276 910.334 555.741 719.999 555.741C529.665 555.741 375.368 650.276 375.368 766.891C375.368 883.505 529.665 978.04 719.999 978.04Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M720 1020.95C1262.17 1020.95 1701.68 756.371 1701.68 430C1701.68 103.629 1262.17 -160.946 720 -160.946C177.834 -160.946 -261.678 103.629 -261.678 430C-261.678 756.371 177.834 1020.95 720 1020.95Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 323.658C910.334 323.658 1064.63 223.814 1064.63 100.649C1064.63 -22.5157 910.334 -122.36 719.999 -122.36C529.665 -122.36 375.368 -22.5157 375.368 100.649C375.368 223.814 529.665 323.658 719.999 323.658Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M720.001 706.676C1080.62 706.676 1372.96 517.507 1372.96 284.155C1372.96 50.8029 1080.62 -138.366 720.001 -138.366C359.384 -138.366 67.046 50.8029 67.046 284.155C67.046 517.507 359.384 706.676 720.001 706.676Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 874.604C1180.69 874.604 1554.15 645.789 1554.15 363.531C1554.15 81.2725 1180.69 -147.543 719.999 -147.543C259.311 -147.543 -114.15 81.2725 -114.15 363.531C-114.15 645.789 259.311 874.604 719.999 874.604Z' stroke='%23E1E2E3' stroke-width='1.5' stroke-miterlimit='10'/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id='clip0_14823_146864'%3E%3Crect width='1440' height='860' fill='white'/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E");
+ background-size: cover;
+ background-position: center;
+ background-repeat: no-repeat;
+ }
+
+ .card {
+ text-align: center;
+ padding: 48px;
+ background: white;
+ border-radius: 8px;
+ border: 1px solid #E1E2E3;
+ max-width: 400px;
+ width: 90%;
+ position: relative;
+ z-index: 1;
+ }
+
+ .logo {
+ width: 48px;
+ height: 48px;
+ margin: 0 auto 24px;
+ display: block;
+ }
+
+ .logo svg {
+ width: 100%;
+ height: 100%;
+ }
+
+ h1 {
+ font-size: 20px;
+ font-weight: 600;
+ color: #101010;
+ margin-bottom: 12px;
+ line-height: 1.2;
+ }
+
+ .subtitle {
+ color: #666;
+ font-size: 12px;
+ margin-top: 10px;
+ margin-bottom: 24px;
+ line-height: 1.5;
+ }
+
+ .close-info {
+ font-size: 12px;
+ color: #999;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ gap: 8px;
+ }
+
+ .spinner {
+ width: 16px;
+ height: 16px;
+ border: 2px solid #E1E2E3;
+ border-top: 2px solid #333;
+ border-radius: 50%;
+ animation: spin 1s linear infinite;
+ }
+
+ @keyframes spin {
+ 0% { transform: rotate(0deg); }
+ 100% { transform: rotate(360deg); }
+ }
+
+ /* Dark mode styles */
+ @media (prefers-color-scheme: dark) {
+ body {
+ background-color: #101010;
+ background-image: url("data:image/svg+xml,%3Csvg width='1440' height='860' viewBox='0 0 1440 860' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cg clip-path='url(%23clip0_14833_149362)'%3E%3Cpath d='M720.001 1003.14C1080.62 1003.14 1372.96 824.028 1372.96 603.083C1372.96 382.138 1080.62 203.026 720.001 203.026C359.384 203.026 67.046 382.138 67.046 603.083C67.046 824.028 359.384 1003.14 720.001 1003.14Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 978.04C910.334 978.04 1064.63 883.505 1064.63 766.891C1064.63 650.276 910.334 555.741 719.999 555.741C529.665 555.741 375.368 650.276 375.368 766.891C375.368 883.505 529.665 978.04 719.999 978.04Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M720 1020.95C1262.17 1020.95 1701.68 756.371 1701.68 430C1701.68 103.629 1262.17 -160.946 720 -160.946C177.834 -160.946 -261.678 103.629 -261.678 430C-261.678 756.371 177.834 1020.95 720 1020.95Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 323.658C910.334 323.658 1064.63 223.814 1064.63 100.649C1064.63 -22.5157 910.334 -122.36 719.999 -122.36C529.665 -122.36 375.368 -22.5157 375.368 100.649C375.368 223.814 529.665 323.658 719.999 323.658Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M720.001 706.676C1080.62 706.676 1372.96 517.507 1372.96 284.155C1372.96 50.8029 1080.62 -138.366 720.001 -138.366C359.384 -138.366 67.046 50.8029 67.046 284.155C67.046 517.507 359.384 706.676 720.001 706.676Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3Cpath d='M719.999 874.604C1180.69 874.604 1554.15 645.789 1554.15 363.531C1554.15 81.2725 1180.69 -147.543 719.999 -147.543C259.311 -147.543 -114.15 81.2725 -114.15 363.531C-114.15 645.789 259.311 874.604 719.999 874.604Z' stroke='%2346484A' stroke-width='1.5' stroke-miterlimit='10'/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id='clip0_14833_149362'%3E%3Crect width='1440' height='860' fill='white'/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E");
+ }
+
+ .card {
+ background-color: #141414;
+ border-color: #202020;
+ }
+
+ h1 {
+ color: #E1E2E3;
+ }
+
+ .subtitle {
+ color: #999;
+ }
+
+ .logo svg path {
+ fill: #E1E2E3;
+ }
+
+ .spinner {
+ border-color: #46484A;
+ border-top-color: #E1E2E3;
+ }
+ }
+ </style>
+</head>
+<body>
+ <div class="card">
+ <div class="logo">
+ <svg width="48" height="48" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <path d="M10.7134 7.30028H7.28759V10.7002H10.7134V7.30028Z" fill="#333"/>
+ <path d="M14.1391 2.81618V0.5H3.86131V2.81618C3.86131 3.41495 3.37266 3.89991 2.76935 3.89991H0.435547V14.1001H2.76935C3.37266 14.1001 3.86131 14.5851 3.86131 15.1838V17.5H14.1391V15.1838C14.1391 14.5851 14.6277 14.1001 15.231 14.1001H17.5648V3.89991H15.231C14.6277 3.89991 14.1391 3.41495 14.1391 2.81618ZM14.1391 13.0159C14.1391 13.6147 13.6504 14.0996 13.0471 14.0996H4.95375C4.35043 14.0996 3.86179 13.6147 3.86179 13.0159V4.98363C3.86179 4.38486 4.35043 3.89991 4.95375 3.89991H13.0471C13.6504 3.89991 14.1391 4.38486 14.1391 4.98363V13.0159Z" fill="#333"/>
+ </svg>
+ </div>
+ <h3>Authorization Successful</h3>
+ <p class="subtitle">You have successfully connected your MCP server.</p>
+ <div class="close-info">
+ <span>You can now close this window.</span>
+ </div>
+ </div>
+</body>
+</html>
+"""
diff --git a/letta/services/mcp/sse_client.py b/letta/services/mcp/sse_client.py
index 91e2515c0..950b4ae05 100644
--- a/letta/services/mcp/sse_client.py
+++ b/letta/services/mcp/sse_client.py
@@ -1,4 +1,7 @@
+from typing import Optional
+
from mcp import ClientSession
+from mcp.client.auth import OAuthClientProvider
from mcp.client.sse import sse_client
from letta.functions.mcp_client.types import SSEServerConfig
@@ -13,6 +16,9 @@ logger = get_logger(__name__)
# TODO: Get rid of Async prefix on this class name once we deprecate old sync code
class AsyncSSEMCPClient(AsyncBaseMCPClient):
+ def __init__(self, server_config: SSEServerConfig, oauth_provider: Optional[OAuthClientProvider] = None):
+ super().__init__(server_config, oauth_provider)
+
async def _initialize_connection(self, server_config: SSEServerConfig) -> None:
headers = {}
if server_config.custom_headers:
@@ -21,7 +27,12 @@ class AsyncSSEMCPClient(AsyncBaseMCPClient):
if server_config.auth_header and server_config.auth_token:
headers[server_config.auth_header] = server_config.auth_token
- sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None)
+ # Use OAuth provider if available, otherwise use regular headers
+ if self.oauth_provider:
+ sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider)
+ else:
+ sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None)
+
sse_transport = await self.exit_stack.enter_async_context(sse_cm)
self.stdio, self.write = sse_transport
diff --git a/letta/services/mcp/streamable_http_client.py b/letta/services/mcp/streamable_http_client.py
index 63c269acf..baf2f7c6a 100644
--- a/letta/services/mcp/streamable_http_client.py
+++ b/letta/services/mcp/streamable_http_client.py
@@ -1,4 +1,7 @@
+from typing import Optional
+
from mcp import ClientSession
+from mcp.client.auth import OAuthClientProvider
from mcp.client.streamable_http import streamablehttp_client
from letta.functions.mcp_client.types import BaseServerConfig, StreamableHTTPServerConfig
@@ -9,10 +12,12 @@ logger = get_logger(__name__)
class AsyncStreamableHTTPMCPClient(AsyncBaseMCPClient):
+ def __init__(self, server_config: StreamableHTTPServerConfig, oauth_provider: Optional[OAuthClientProvider] = None):
+ super().__init__(server_config, oauth_provider)
+
async def _initialize_connection(self, server_config: BaseServerConfig) -> None:
if not isinstance(server_config, StreamableHTTPServerConfig):
raise ValueError("Expected StreamableHTTPServerConfig")
-
try:
# Prepare headers for authentication
headers = {}
@@ -23,11 +28,18 @@ class AsyncStreamableHTTPMCPClient(AsyncBaseMCPClient):
if server_config.auth_header and server_config.auth_token:
headers[server_config.auth_header] = server_config.auth_token
- # Use streamablehttp_client context manager with headers if provided
- if headers:
- streamable_http_cm = streamablehttp_client(server_config.server_url, headers=headers)
+ # Use OAuth provider if available, otherwise use regular headers
+ if self.oauth_provider:
+ streamable_http_cm = streamablehttp_client(
+ server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider
+ )
else:
- streamable_http_cm = streamablehttp_client(server_config.server_url)
+ # Use streamablehttp_client context manager with headers if provided
+ if headers:
+ streamable_http_cm = streamablehttp_client(server_config.server_url, headers=headers)
+ else:
+ streamable_http_cm = streamablehttp_client(server_config.server_url)
+
read_stream, write_stream, _ = await self.exit_stack.enter_async_context(streamable_http_cm)
# Create and enter the ClientSession context manager
diff --git a/letta/services/mcp/types.py b/letta/services/mcp/types.py
index 2d8b7af6d..b5e873cb7 100644
--- a/letta/services/mcp/types.py
+++ b/letta/services/mcp/types.py
@@ -46,3 +46,12 @@ class StdioServerConfig(BaseServerConfig):
if self.env is not None:
values["env"] = self.env
return values
+
+
+class OauthStreamEvent(str, Enum):
+ CONNECTION_ATTEMPT = "connection_attempt"
+ SUCCESS = "success"
+ ERROR = "error"
+ OAUTH_REQUIRED = "oauth_required"
+ AUTHORIZATION_URL = "authorization_url"
+ WAITING_FOR_AUTH = "waiting_for_auth"
diff --git a/letta/services/mcp_manager.py b/letta/services/mcp_manager.py
index b542e22bf..e03618220 100644
--- a/letta/services/mcp_manager.py
+++ b/letta/services/mcp_manager.py
@@ -1,15 +1,29 @@
import json
import os
+import secrets
+import uuid
+from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple, Union
+from fastapi import HTTPException
from sqlalchemy import null
import letta.constants as constants
from letta.functions.mcp_client.types import MCPServerType, MCPTool, SSEServerConfig, StdioServerConfig, StreamableHTTPServerConfig
from letta.log import get_logger
from letta.orm.errors import NoResultFound
+from letta.orm.mcp_oauth import MCPOAuth, OAuthSessionStatus
from letta.orm.mcp_server import MCPServer as MCPServerModel
-from letta.schemas.mcp import MCPServer, UpdateMCPServer, UpdateSSEMCPServer, UpdateStdioMCPServer, UpdateStreamableHTTPMCPServer
+from letta.schemas.mcp import (
+ MCPOAuthSession,
+ MCPOAuthSessionCreate,
+ MCPOAuthSessionUpdate,
+ MCPServer,
+ UpdateMCPServer,
+ UpdateSSEMCPServer,
+ UpdateStdioMCPServer,
+ UpdateStreamableHTTPMCPServer,
+)
from letta.schemas.tool import Tool as PydanticTool
from letta.schemas.tool import ToolCreate
from letta.schemas.user import User as PydanticUser
@@ -34,27 +48,21 @@ class MCPManager:
@enforce_types
async def list_mcp_server_tools(self, mcp_server_name: str, actor: PydanticUser) -> List[MCPTool]:
"""Get a list of all tools for a specific MCP server."""
- mcp_server_id = await self.get_mcp_server_id_by_name(mcp_server_name, actor=actor)
- mcp_config = await self.get_mcp_server_by_id_async(mcp_server_id, actor=actor)
- server_config = mcp_config.to_config()
-
- if mcp_config.server_type == MCPServerType.SSE:
- mcp_client = AsyncSSEMCPClient(server_config=server_config)
- elif mcp_config.server_type == MCPServerType.STDIO:
- mcp_client = AsyncStdioMCPClient(server_config=server_config)
- elif mcp_config.server_type == MCPServerType.STREAMABLE_HTTP:
- mcp_client = AsyncStreamableHTTPMCPClient(server_config=server_config)
- else:
- raise ValueError(f"Unsupported MCP server type: {mcp_config.server_type}")
- await mcp_client.connect_to_server()
-
- # list tools
- tools = await mcp_client.list_tools()
-
- # TODO: change to pydantic tools
- await mcp_client.cleanup()
-
- return tools
+ try:
+ mcp_server_id = await self.get_mcp_server_id_by_name(mcp_server_name, actor=actor)
+ mcp_config = await self.get_mcp_server_by_id_async(mcp_server_id, actor=actor)
+ server_config = mcp_config.to_config()
+ mcp_client = await self.get_mcp_client(server_config, actor)
+ await mcp_client.connect_to_server()
+
+ # list tools
+ tools = await mcp_client.list_tools()
+ return tools
+ except Exception as e:
+ logger.error(f"Error listing tools for MCP server {mcp_server_name}: {e}")
+ return []
+ finally:
+ await mcp_client.cleanup()
@enforce_types
async def execute_mcp_server_tool(
@@ -72,38 +80,37 @@ class MCPManager:
# read from config file
mcp_config = self.read_mcp_config()
if mcp_server_name not in mcp_config:
- print("MCP server not found in config.", mcp_config)
raise ValueError(f"MCP server {mcp_server_name} not found in config.")
server_config = mcp_config[mcp_server_name]
- if isinstance(server_config, SSEServerConfig):
- # mcp_client = AsyncSSEMCPClient(server_config=server_config)
- async with AsyncSSEMCPClient(server_config=server_config) as mcp_client:
- result, success = await mcp_client.execute_tool(tool_name, tool_args)
- logger.info(f"MCP Result: {result}, Success: {success}")
- return result, success
- elif isinstance(server_config, StdioServerConfig):
- async with AsyncStdioMCPClient(server_config=server_config) as mcp_client:
- result, success = await mcp_client.execute_tool(tool_name, tool_args)
- logger.info(f"MCP Result: {result}, Success: {success}")
- return result, success
- elif isinstance(server_config, StreamableHTTPServerConfig):
- async with AsyncStreamableHTTPMCPClient(server_config=server_config) as mcp_client:
- result, success = await mcp_client.execute_tool(tool_name, tool_args)
- logger.info(f"MCP Result: {result}, Success: {success}")
- return result, success
- else:
- raise ValueError(f"Unsupported server config type: {type(server_config)}")
+ mcp_client = await self.get_mcp_client(server_config, actor)
+ await mcp_client.connect_to_server()
+
+ # call tool
+ result, success = await mcp_client.execute_tool(tool_name, tool_args)
+ logger.info(f"MCP Result: {result}, Success: {success}")
+ # TODO: change to pydantic tool
+
+ await mcp_client.cleanup()
+
+ return result, success
@enforce_types
async def add_tool_from_mcp_server(self, mcp_server_name: str, mcp_tool_name: str, actor: PydanticUser) -> PydanticTool:
"""Add a tool from an MCP server to the Letta tool registry."""
+ # get the MCP server ID, we should migrate to use the server_id instead of the name
+ mcp_server_id = await self.get_mcp_server_id_by_name(mcp_server_name, actor=actor)
+ if not mcp_server_id:
+ raise ValueError(f"MCP server '{mcp_server_name}' not found")
+
mcp_tools = await self.list_mcp_server_tools(mcp_server_name, actor=actor)
for mcp_tool in mcp_tools:
if mcp_tool.name == mcp_tool_name:
tool_create = ToolCreate.from_mcp(mcp_server_name=mcp_server_name, mcp_tool=mcp_tool)
- return await self.tool_manager.create_mcp_tool_async(tool_create=tool_create, mcp_server_name=mcp_server_name, actor=actor)
+ return await self.tool_manager.create_mcp_tool_async(
+ tool_create=tool_create, mcp_server_name=mcp_server_name, mcp_server_id=mcp_server_id, actor=actor
+ )
# failed to add - handle error?
return None
@@ -223,6 +230,18 @@ class MCPManager:
# Convert the SQLAlchemy Tool object to PydanticTool
return mcp_server.to_pydantic()
+ @enforce_types
+ async def get_mcp_servers_by_ids(self, mcp_server_ids: List[str], actor: PydanticUser) -> List[MCPServer]:
+ """Fetch multiple MCP servers by their IDs in a single query."""
+ if not mcp_server_ids:
+ return []
+
+ async with db_registry.async_session() as session:
+ mcp_servers = await MCPServerModel.list_async(
+ db_session=session, organization_id=actor.organization_id, id=mcp_server_ids # This will use the IN operator
+ )
+ return [mcp_server.to_pydantic() for mcp_server in mcp_servers]
+
@enforce_types
async def get_mcp_server(self, mcp_server_name: str, actor: PydanticUser) -> PydanticTool:
"""Get a tool by name."""
@@ -319,3 +338,246 @@ class MCPManager:
logger.error(f"Failed to parse server params for MCP server {server_name} (skipping): {e}")
continue
return mcp_server_list
+
+ async def get_mcp_client(
+ self,
+ server_config: Union[SSEServerConfig, StdioServerConfig, StreamableHTTPServerConfig],
+ actor: PydanticUser,
+ oauth_provider: Optional[Any] = None,
+ ) -> Union[AsyncSSEMCPClient, AsyncStdioMCPClient, AsyncStreamableHTTPMCPClient]:
+ """
+ Helper function to create the appropriate MCP client based on server configuration.
+
+ Args:
+ server_config: The server configuration object
+ actor: The user making the request
+ oauth_provider: Optional OAuth provider for authentication
+
+ Returns:
+ The appropriate MCP client instance
+
+ Raises:
+ ValueError: If server config type is not supported
+ """
+ # If no OAuth provider is provided, check if we have stored OAuth credentials
+ if oauth_provider is None and hasattr(server_config, "server_url"):
+ oauth_session = await self.get_oauth_session_by_server(server_config.server_url, actor)
+ if oauth_session and oauth_session.access_token:
+ # Create OAuth provider from stored credentials
+ from letta.services.mcp.oauth_utils import create_oauth_provider
+
+ oauth_provider = await create_oauth_provider(
+ session_id=oauth_session.id,
+ server_url=oauth_session.server_url,
+ redirect_uri=oauth_session.redirect_uri,
+ mcp_manager=self,
+ actor=actor,
+ )
+
+ if server_config.type == MCPServerType.SSE:
+ server_config = SSEServerConfig(**server_config.model_dump())
+ return AsyncSSEMCPClient(server_config=server_config, oauth_provider=oauth_provider)
+ elif server_config.type == MCPServerType.STDIO:
+ server_config = StdioServerConfig(**server_config.model_dump())
+ return AsyncStdioMCPClient(server_config=server_config, oauth_provider=oauth_provider)
+ elif server_config.type == MCPServerType.STREAMABLE_HTTP:
+ server_config = StreamableHTTPServerConfig(**server_config.model_dump())
+ return AsyncStreamableHTTPMCPClient(server_config=server_config, oauth_provider=oauth_provider)
+ else:
+ raise ValueError(f"Unsupported server config type: {type(server_config)}")
+
+ # OAuth-related methods
+ @enforce_types
+ async def create_oauth_session(self, session_create: MCPOAuthSessionCreate, actor: PydanticUser) -> MCPOAuthSession:
+ """Create a new OAuth session for MCP server authentication."""
+ async with db_registry.async_session() as session:
+ # Create the OAuth session with a unique state
+ oauth_session = MCPOAuth(
+ id="mcp-oauth-" + str(uuid.uuid4())[:8],
+ state=secrets.token_urlsafe(32),
+ server_url=session_create.server_url,
+ server_name=session_create.server_name,
+ user_id=session_create.user_id,
+ organization_id=session_create.organization_id,
+ status=OAuthSessionStatus.PENDING,
+ created_at=datetime.now(),
+ updated_at=datetime.now(),
+ )
+ oauth_session = await oauth_session.create_async(session, actor=actor)
+
+ # Convert to Pydantic model
+ return MCPOAuthSession(
+ id=oauth_session.id,
+ state=oauth_session.state,
+ server_url=oauth_session.server_url,
+ server_name=oauth_session.server_name,
+ user_id=oauth_session.user_id,
+ organization_id=oauth_session.organization_id,
+ status=oauth_session.status,
+ created_at=oauth_session.created_at,
+ updated_at=oauth_session.updated_at,
+ )
+
+ @enforce_types
+ async def get_oauth_session_by_id(self, session_id: str, actor: PydanticUser) -> Optional[MCPOAuthSession]:
+ """Get an OAuth session by its ID."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_session = await MCPOAuth.read_async(db_session=session, identifier=session_id, actor=actor)
+ return MCPOAuthSession(
+ id=oauth_session.id,
+ state=oauth_session.state,
+ server_url=oauth_session.server_url,
+ server_name=oauth_session.server_name,
+ user_id=oauth_session.user_id,
+ organization_id=oauth_session.organization_id,
+ authorization_url=oauth_session.authorization_url,
+ authorization_code=oauth_session.authorization_code,
+ access_token=oauth_session.access_token,
+ refresh_token=oauth_session.refresh_token,
+ token_type=oauth_session.token_type,
+ expires_at=oauth_session.expires_at,
+ scope=oauth_session.scope,
+ client_id=oauth_session.client_id,
+ client_secret=oauth_session.client_secret,
+ redirect_uri=oauth_session.redirect_uri,
+ status=oauth_session.status,
+ created_at=oauth_session.created_at,
+ updated_at=oauth_session.updated_at,
+ )
+ except NoResultFound:
+ return None
+
+ @enforce_types
+ async def get_oauth_session_by_server(self, server_url: str, actor: PydanticUser) -> Optional[MCPOAuthSession]:
+ """Get the latest OAuth session by server URL, organization, and user."""
+ from sqlalchemy import desc, select
+
+ async with db_registry.async_session() as session:
+ # Query for OAuth session matching organization, user, server URL, and status
+ # Order by updated_at desc to get the most recent record
+ result = await session.execute(
+ select(MCPOAuth)
+ .where(
+ MCPOAuth.organization_id == actor.organization_id,
+ MCPOAuth.user_id == actor.id,
+ MCPOAuth.server_url == server_url,
+ MCPOAuth.status == OAuthSessionStatus.AUTHORIZED,
+ )
+ .order_by(desc(MCPOAuth.updated_at))
+ .limit(1)
+ )
+ oauth_session = result.scalar_one_or_none()
+
+ if not oauth_session:
+ return None
+
+ return MCPOAuthSession(
+ id=oauth_session.id,
+ state=oauth_session.state,
+ server_url=oauth_session.server_url,
+ server_name=oauth_session.server_name,
+ user_id=oauth_session.user_id,
+ organization_id=oauth_session.organization_id,
+ authorization_url=oauth_session.authorization_url,
+ authorization_code=oauth_session.authorization_code,
+ access_token=oauth_session.access_token,
+ refresh_token=oauth_session.refresh_token,
+ token_type=oauth_session.token_type,
+ expires_at=oauth_session.expires_at,
+ scope=oauth_session.scope,
+ client_id=oauth_session.client_id,
+ client_secret=oauth_session.client_secret,
+ redirect_uri=oauth_session.redirect_uri,
+ status=oauth_session.status,
+ created_at=oauth_session.created_at,
+ updated_at=oauth_session.updated_at,
+ )
+
+ @enforce_types
+ async def update_oauth_session(self, session_id: str, session_update: MCPOAuthSessionUpdate, actor: PydanticUser) -> MCPOAuthSession:
+ """Update an existing OAuth session."""
+ async with db_registry.async_session() as session:
+ oauth_session = await MCPOAuth.read_async(db_session=session, identifier=session_id, actor=actor)
+
+ # Update fields that are provided
+ if session_update.authorization_url is not None:
+ oauth_session.authorization_url = session_update.authorization_url
+ if session_update.authorization_code is not None:
+ oauth_session.authorization_code = session_update.authorization_code
+ if session_update.access_token is not None:
+ oauth_session.access_token = session_update.access_token
+ if session_update.refresh_token is not None:
+ oauth_session.refresh_token = session_update.refresh_token
+ if session_update.token_type is not None:
+ oauth_session.token_type = session_update.token_type
+ if session_update.expires_at is not None:
+ oauth_session.expires_at = session_update.expires_at
+ if session_update.scope is not None:
+ oauth_session.scope = session_update.scope
+ if session_update.client_id is not None:
+ oauth_session.client_id = session_update.client_id
+ if session_update.client_secret is not None:
+ oauth_session.client_secret = session_update.client_secret
+ if session_update.redirect_uri is not None:
+ oauth_session.redirect_uri = session_update.redirect_uri
+ if session_update.status is not None:
+ oauth_session.status = session_update.status
+
+ # Always update the updated_at timestamp
+ oauth_session.updated_at = datetime.now()
+
+ oauth_session = await oauth_session.update_async(db_session=session, actor=actor)
+
+ return MCPOAuthSession(
+ id=oauth_session.id,
+ state=oauth_session.state,
+ server_url=oauth_session.server_url,
+ server_name=oauth_session.server_name,
+ user_id=oauth_session.user_id,
+ organization_id=oauth_session.organization_id,
+ authorization_url=oauth_session.authorization_url,
+ authorization_code=oauth_session.authorization_code,
+ access_token=oauth_session.access_token,
+ refresh_token=oauth_session.refresh_token,
+ token_type=oauth_session.token_type,
+ expires_at=oauth_session.expires_at,
+ scope=oauth_session.scope,
+ client_id=oauth_session.client_id,
+ client_secret=oauth_session.client_secret,
+ redirect_uri=oauth_session.redirect_uri,
+ status=oauth_session.status,
+ created_at=oauth_session.created_at,
+ updated_at=oauth_session.updated_at,
+ )
+
+ @enforce_types
+ async def delete_oauth_session(self, session_id: str, actor: PydanticUser) -> None:
+ """Delete an OAuth session."""
+ async with db_registry.async_session() as session:
+ try:
+ oauth_session = await MCPOAuth.read_async(db_session=session, identifier=session_id, actor=actor)
+ await oauth_session.hard_delete_async(db_session=session, actor=actor)
+ except NoResultFound:
+ raise ValueError(f"OAuth session with id {session_id} not found.")
+
+ @enforce_types
+ async def cleanup_expired_oauth_sessions(self, max_age_hours: int = 24) -> int:
+ """Clean up expired OAuth sessions and return the count of deleted sessions."""
+ cutoff_time = datetime.now() - timedelta(hours=max_age_hours)
+
+ async with db_registry.async_session() as session:
+ from sqlalchemy import select
+
+ # Find expired sessions
+ result = await session.execute(select(MCPOAuth).where(MCPOAuth.created_at < cutoff_time))
+ expired_sessions = result.scalars().all()
+
+ # Delete expired sessions using async ORM method
+ for oauth_session in expired_sessions:
+ await oauth_session.hard_delete_async(db_session=session, actor=None)
+
+ if expired_sessions:
+ logger.info(f"Cleaned up {len(expired_sessions)} expired OAuth sessions")
+
+ return len(expired_sessions)
diff --git a/letta/services/provider_manager.py b/letta/services/provider_manager.py
index 675ff7638..46fe615e3 100644
--- a/letta/services/provider_manager.py
+++ b/letta/services/provider_manager.py
@@ -213,12 +213,12 @@ class ProviderManager:
provider_type=provider_check.provider_type,
api_key=provider_check.api_key,
provider_category=ProviderCategory.byok,
- access_id_key=provider_check.access_id_key, # This contains the access key ID for Bedrock
+ access_key=provider_check.access_key, # This contains the access key ID for Bedrock
region=provider_check.region,
).cast_to_subtype()
# TODO: add more string sanity checks here before we hit actual endpoints
if not provider.api_key:
- raise ValueError("API key is required")
+ raise ValueError("API key is required!")
await provider.check_api_key()
diff --git a/letta/services/tool_executor/tool_executor.py b/letta/services/tool_executor/tool_executor.py
index b57d87cbf..9476e498d 100644
--- a/letta/services/tool_executor/tool_executor.py
+++ b/letta/services/tool_executor/tool_executor.py
@@ -36,7 +36,10 @@ class SandboxToolExecutor(ToolExecutor):
) -> ToolExecutionResult:
# Store original memory state
- orig_memory_str = agent_state.memory.compile() if agent_state else None
+ if agent_state:
+ orig_memory_str = await agent_state.memory.compile_async()
+ else:
+ orig_memory_str = None
try:
# Prepare function arguments
@@ -58,7 +61,8 @@ class SandboxToolExecutor(ToolExecutor):
# Verify memory integrity
if agent_state:
- assert orig_memory_str == agent_state.memory.compile(), "Memory should not be modified in a sandbox tool"
+ new_memory_str = await agent_state.memory.compile_async()
+ assert orig_memory_str == new_memory_str, "Memory should not be modified in a sandbox tool"
# Update agent memory if needed
if tool_execution_result.agent_state is not None:
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index 1885fa24f..29d2723ad 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -106,8 +106,10 @@ class ToolManager:
@enforce_types
@trace_method
- def create_or_update_mcp_tool(self, tool_create: ToolCreate, mcp_server_name: str, actor: PydanticUser) -> PydanticTool:
- metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name}}
+ def create_or_update_mcp_tool(
+ self, tool_create: ToolCreate, mcp_server_name: str, mcp_server_id: str, actor: PydanticUser
+ ) -> PydanticTool:
+ metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name, "server_id": mcp_server_id}}
return self.create_or_update_tool(
PydanticTool(
tool_type=ToolType.EXTERNAL_MCP, name=tool_create.json_schema["name"], metadata_=metadata, **tool_create.model_dump()
@@ -116,8 +118,10 @@ class ToolManager:
)
@enforce_types
- async def create_mcp_tool_async(self, tool_create: ToolCreate, mcp_server_name: str, actor: PydanticUser) -> PydanticTool:
- metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name}}
+ async def create_mcp_tool_async(
+ self, tool_create: ToolCreate, mcp_server_name: str, mcp_server_id: str, actor: PydanticUser
+ ) -> PydanticTool:
+ metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name, "server_id": mcp_server_id}}
return await self.create_or_update_tool_async(
PydanticTool(
tool_type=ToolType.EXTERNAL_MCP, name=tool_create.json_schema["name"], metadata_=metadata, **tool_create.model_dump()
diff --git a/letta/services/tool_sandbox/base.py b/letta/services/tool_sandbox/base.py
index 030839817..68e4d1099 100644
--- a/letta/services/tool_sandbox/base.py
+++ b/letta/services/tool_sandbox/base.py
@@ -74,12 +74,12 @@ class AsyncToolSandboxBase(ABC):
"""
raise NotImplementedError
- def generate_execution_script(self, agent_state: Optional[AgentState], wrap_print_with_markers: bool = False) -> str:
+ async def generate_execution_script(self, agent_state: Optional[AgentState], wrap_print_with_markers: bool = False) -> str:
"""
Generate code to run inside of execution sandbox. Serialize the agent state and arguments, call the tool,
then base64-encode/pickle the result. Runs a jinja2 template constructing the python file.
"""
- from letta.templates.template_helper import render_template
+ from letta.templates.template_helper import render_template_async
# Select the appropriate template based on whether the function is async
TEMPLATE_NAME = "sandbox_code_file_async.py.j2" if self.is_async_function else "sandbox_code_file.py.j2"
@@ -106,7 +106,7 @@ class AsyncToolSandboxBase(ABC):
agent_state_pickle = pickle.dumps(agent_state) if self.inject_agent_state else None
- return render_template(
+ return await render_template_async(
TEMPLATE_NAME,
future_import=future_import,
inject_agent_state=self.inject_agent_state,
diff --git a/letta/services/tool_sandbox/e2b_sandbox.py b/letta/services/tool_sandbox/e2b_sandbox.py
index ca7ca907e..bdc65e5c9 100644
--- a/letta/services/tool_sandbox/e2b_sandbox.py
+++ b/letta/services/tool_sandbox/e2b_sandbox.py
@@ -92,7 +92,7 @@ class AsyncToolSandboxE2B(AsyncToolSandboxBase):
# Finally, get any that are passed explicitly into the `run` function call
if additional_env_vars:
env_vars.update(additional_env_vars)
- code = self.generate_execution_script(agent_state=agent_state)
+ code = await self.generate_execution_script(agent_state=agent_state)
try:
log_event(
diff --git a/letta/services/tool_sandbox/local_sandbox.py b/letta/services/tool_sandbox/local_sandbox.py
index 5056adde5..8f2b28714 100644
--- a/letta/services/tool_sandbox/local_sandbox.py
+++ b/letta/services/tool_sandbox/local_sandbox.py
@@ -99,8 +99,8 @@ class AsyncToolSandboxLocal(AsyncToolSandboxBase):
# Make sure sandbox directory exists
sandbox_dir = os.path.expanduser(local_configs.sandbox_dir)
- if not os.path.exists(sandbox_dir) or not os.path.isdir(sandbox_dir):
- os.makedirs(sandbox_dir)
+ if not await asyncio.to_thread(lambda: os.path.exists(sandbox_dir) and os.path.isdir(sandbox_dir)):
+ await asyncio.to_thread(os.makedirs, sandbox_dir)
# If using a virtual environment, ensure it's prepared in parallel
venv_preparation_task = None
@@ -109,11 +109,18 @@ class AsyncToolSandboxLocal(AsyncToolSandboxBase):
venv_preparation_task = asyncio.create_task(self._prepare_venv(local_configs, venv_path, env))
# Generate and write execution script (always with markers, since we rely on stdout)
- with tempfile.NamedTemporaryFile(mode="w", dir=sandbox_dir, suffix=".py", delete=False) as temp_file:
- code = self.generate_execution_script(agent_state=agent_state, wrap_print_with_markers=True)
- temp_file.write(code)
- temp_file.flush()
- temp_file_path = temp_file.name
+ code = await self.generate_execution_script(agent_state=agent_state, wrap_print_with_markers=True)
+
+ async def write_temp_file(dir, content):
+ def _write():
+ with tempfile.NamedTemporaryFile(mode="w", dir=dir, suffix=".py", delete=False) as temp_file:
+ temp_file.write(content)
+ temp_file.flush()
+ return temp_file.name
+
+ return await asyncio.to_thread(_write)
+
+ temp_file_path = await write_temp_file(sandbox_dir, code)
try:
# If we started a venv preparation task, wait for it to complete
@@ -159,14 +166,14 @@ class AsyncToolSandboxLocal(AsyncToolSandboxBase):
from letta.settings import settings
if not settings.debug:
- os.remove(temp_file_path)
+ await asyncio.to_thread(os.remove, temp_file_path)
@trace_method
async def _prepare_venv(self, local_configs, venv_path: str, env: Dict[str, str]):
"""
Prepare virtual environment asynchronously (in a background thread).
"""
- if self.force_recreate_venv or not os.path.isdir(venv_path):
+ if self.force_recreate_venv or not await asyncio.to_thread(os.path.isdir, venv_path):
sandbox_dir = os.path.expanduser(local_configs.sandbox_dir)
log_event(name="start create_venv_for_local_sandbox", attributes={"venv_path": venv_path})
await asyncio.to_thread(
diff --git a/letta/settings.py b/letta/settings.py
index 1d2005524..16110d04d 100644
--- a/letta/settings.py
+++ b/letta/settings.py
@@ -242,7 +242,7 @@ class Settings(BaseSettings):
uvicorn_reload: bool = False
uvicorn_timeout_keep_alive: int = 5
- use_uvloop: bool = Field(default=True, description="Enable uvloop as asyncio event loop.")
+ use_uvloop: bool = Field(default=False, description="Enable uvloop as asyncio event loop.")
use_granian: bool = Field(default=False, description="Use Granian for workers")
sqlalchemy_tracing: bool = False
@@ -278,6 +278,10 @@ class Settings(BaseSettings):
pinecone_agent_index: Optional[str] = "recall"
upsert_pinecone_indices: bool = False
+ # File processing timeout settings
+ file_processing_timeout_minutes: int = 30
+ file_processing_timeout_error_message: str = "File processing timed out after {} minutes. Please try again."
+
@property
def letta_pg_uri(self) -> str:
if self.pg_uri:
@@ -327,6 +331,11 @@ class LogSettings(BaseSettings):
verbose_telemetry_logging: bool = Field(False)
+class TelemetrySettings(BaseSettings):
+ model_config = SettingsConfigDict(env_prefix="letta_telemetry_", extra="ignore")
+ profiler: bool | None = Field(False, description="Enable use of the profiler.")
+
+
# singleton
settings = Settings(_env_parse_none_str="None")
test_settings = TestSettings()
@@ -334,3 +343,4 @@ model_settings = ModelSettings()
tool_settings = ToolSettings()
summarizer_settings = SummarizerSettings()
log_settings = LogSettings()
+telemetry_settings = TelemetrySettings()
diff --git a/letta/system.py b/letta/system.py
index 888cc304a..f64cc9d0b 100644
--- a/letta/system.py
+++ b/letta/system.py
@@ -141,7 +141,7 @@ def package_user_message(
return json_dumps(packaged_message)
-def package_function_response(was_success, response_string, timezone):
+def package_function_response(was_success: bool, response_string: str, timezone: str | None) -> str:
formatted_time = get_local_time(timezone=timezone)
packaged_message = {
"status": "OK" if was_success else "Failed",
diff --git a/letta/templates/template_helper.py b/letta/templates/template_helper.py
index 0d2359cef..428e2bd22 100644
--- a/letta/templates/template_helper.py
+++ b/letta/templates/template_helper.py
@@ -1,8 +1,10 @@
import os
-from jinja2 import Environment, FileSystemLoader, StrictUndefined
+from jinja2 import Environment, FileSystemLoader, StrictUndefined, Template
TEMPLATE_DIR = os.path.dirname(__file__)
+
+# Synchronous environment (for backward compatibility)
jinja_env = Environment(
loader=FileSystemLoader(TEMPLATE_DIR),
undefined=StrictUndefined,
@@ -10,7 +12,29 @@ jinja_env = Environment(
lstrip_blocks=True,
)
+# Async-enabled environment
+jinja_async_env = Environment(
+ loader=FileSystemLoader(TEMPLATE_DIR),
+ undefined=StrictUndefined,
+ trim_blocks=True,
+ lstrip_blocks=True,
+ enable_async=True, # Enable async support
+)
+
def render_template(template_name: str, **kwargs):
+ """Synchronous template rendering function (kept for backward compatibility)"""
template = jinja_env.get_template(template_name)
return template.render(**kwargs)
+
+
+async def render_template_async(template_name: str, **kwargs):
+ """Asynchronous template rendering function that doesn't block the event loop"""
+ template = jinja_async_env.get_template(template_name)
+ return await template.render_async(**kwargs)
+
+
+async def render_string_async(template_string: str, **kwargs):
+ """Asynchronously render a template from a string"""
+ template = Template(template_string, enable_async=True)
+ return await template.render_async(**kwargs)
diff --git a/letta/utils.py b/letta/utils.py
index 2b8a45019..73abb1a33 100644
--- a/letta/utils.py
+++ b/letta/utils.py
@@ -27,7 +27,6 @@ from sqlalchemy import text
import letta
from letta.constants import (
- CLI_WARNING_PREFIX,
CORE_MEMORY_HUMAN_CHAR_LIMIT,
CORE_MEMORY_PERSONA_CHAR_LIMIT,
DEFAULT_CORE_MEMORY_SOURCE_CHAR_LIMIT,
@@ -851,47 +850,32 @@ def parse_json(string) -> dict:
raise e
-def validate_function_response(function_response_string: any, return_char_limit: int, strict: bool = False, truncate: bool = True) -> str:
+def validate_function_response(function_response: Any, return_char_limit: int, strict: bool = False, truncate: bool = True) -> str:
"""Check to make sure that a function used by Letta returned a valid response. Truncates to return_char_limit if necessary.
- Responses need to be strings (or None) that fall under a certain text count limit.
+ This makes sure that we can coerce the function_response into a string that meets our criteria. We handle some soft coercion.
+ If strict is True, we raise a ValueError if function_response is not a string or None.
"""
- if not isinstance(function_response_string, str):
- # Soft correction for a few basic types
+ if isinstance(function_response, str):
+ function_response_string = function_response
- if function_response_string is None:
- # function_response_string = "Empty (no function output)"
- function_response_string = "None" # backcompat
+ elif function_response is None:
+ function_response_string = "None"
- elif isinstance(function_response_string, dict):
- if strict:
- # TODO add better error message
- raise ValueError(function_response_string)
+ elif strict:
+ raise ValueError(f"Strict mode violation. Function returned type: {type(function_response).__name__}")
- # Allow dict through since it will be cast to json.dumps()
- try:
- # TODO find a better way to do this that won't result in double escapes
- function_response_string = json_dumps(function_response_string)
- except:
- raise ValueError(function_response_string)
-
- else:
- if strict:
- # TODO add better error message
- raise ValueError(function_response_string)
-
- # Try to convert to a string, but throw a warning to alert the user
- try:
- function_response_string = str(function_response_string)
- except:
- raise ValueError(function_response_string)
+ elif isinstance(function_response, dict):
+ # As functions can return arbitrary data, if there's already nesting somewhere in the response, it's difficult
+ # for us to not result in double escapes.
+ function_response_string = json_dumps(function_response)
+ else:
+ logger.debug(f"Function returned type {type(function_response).__name__}. Coercing to string.")
+ function_response_string = str(function_response)
- # Now check the length and make sure it doesn't go over the limit
# TODO we should change this to a max token limit that's variable based on tokens remaining (or context-window)
if truncate and len(function_response_string) > return_char_limit:
- print(
- f"{CLI_WARNING_PREFIX}function return was over limit ({len(function_response_string)} > {return_char_limit}) and was truncated"
- )
+ logger.warning(f"function return was over limit ({len(function_response_string)} > {return_char_limit}) and was truncated")
function_response_string = f"{function_response_string[:return_char_limit]}... [NOTE: function output was truncated since it exceeded the character limit ({len(function_response_string)} > {return_char_limit})]"
return function_response_string
@@ -1236,6 +1220,6 @@ def calculate_file_defaults_based_on_context_window(context_window: Optional[int
elif context_window <= 128_000: # Large models (100K-128K)
return 10, 25_000 # ~62.5K tokens
elif context_window <= 200_000: # Very large models (128K-200K)
- return 10, 50_000 # ~128k tokens
+ return 10, 40_000 # ~100k tokens
else: # Extremely large models (200K+)
- return 15, 50_000 # ~187.5k tokens
+ return 15, 40_000 # ~1505k tokens
diff --git a/mcp_test.py b/mcp_test.py
new file mode 100644
index 000000000..507d5c7c6
--- /dev/null
+++ b/mcp_test.py
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+"""
+Simple MCP client example with OAuth authentication support.
+
+This client connects to an MCP server using streamable HTTP transport with OAuth.
+
+"""
+
+import asyncio
+import os
+import threading
+import time
+import webbrowser
+from datetime import timedelta
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from typing import Any
+from urllib.parse import parse_qs, urlparse
+
+from mcp.client.auth import OAuthClientProvider, TokenStorage
+from mcp.client.session import ClientSession
+from mcp.client.sse import sse_client
+from mcp.client.streamable_http import streamablehttp_client
+from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
+
+
+class InMemoryTokenStorage(TokenStorage):
+ """Simple in-memory token storage implementation."""
+
+ def __init__(self):
+ self._tokens: OAuthToken | None = None
+ self._client_info: OAuthClientInformationFull | None = None
+
+ async def get_tokens(self) -> OAuthToken | None:
+ return self._tokens
+
+ async def set_tokens(self, tokens: OAuthToken) -> None:
+ self._tokens = tokens
+
+ async def get_client_info(self) -> OAuthClientInformationFull | None:
+ return self._client_info
+
+ async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
+ self._client_info = client_info
+
+
+class CallbackHandler(BaseHTTPRequestHandler):
+ """Simple HTTP handler to capture OAuth callback."""
+
+ def __init__(self, request, client_address, server, callback_data):
+ """Initialize with callback data storage."""
+ self.callback_data = callback_data
+ super().__init__(request, client_address, server)
+
+ def do_GET(self):
+ """Handle GET request from OAuth redirect."""
+ parsed = urlparse(self.path)
+ query_params = parse_qs(parsed.query)
+
+ if "code" in query_params:
+ self.callback_data["authorization_code"] = query_params["code"][0]
+ self.callback_data["state"] = query_params.get("state", [None])[0]
+ self.send_response(200)
+ self.send_header("Content-type", "text/html")
+ self.end_headers()
+ self.wfile.write(
+ b"""
+ <html>
+ <body>
+ <h1>Authorization Successful!</h1>
+ <p>You can close this window and return to the terminal.</p>
+ <script>setTimeout(() => window.close(), 2000);</script>
+ </body>
+ </html>
+ """
+ )
+ elif "error" in query_params:
+ self.callback_data["error"] = query_params["error"][0]
+ self.send_response(400)
+ self.send_header("Content-type", "text/html")
+ self.end_headers()
+ self.wfile.write(
+ f"""
+ <html>
+ <body>
+ <h1>Authorization Failed</h1>
+ <p>Error: {query_params['error'][0]}</p>
+ <p>You can close this window and return to the terminal.</p>
+ </body>
+ </html>
+ """.encode()
+ )
+ else:
+ self.send_response(404)
+ self.end_headers()
+
+ def log_message(self, format, *args):
+ """Suppress default logging."""
+
+
+class CallbackServer:
+ """Simple server to handle OAuth callbacks."""
+
+ def __init__(self, port=3000):
+ self.port = port
+ self.server = None
+ self.thread = None
+ self.callback_data = {"authorization_code": None, "state": None, "error": None}
+
+ def _create_handler_with_data(self):
+ """Create a handler class with access to callback data."""
+ callback_data = self.callback_data
+
+ class DataCallbackHandler(CallbackHandler):
+ def __init__(self, request, client_address, server):
+ super().__init__(request, client_address, server, callback_data)
+
+ return DataCallbackHandler
+
+ def start(self):
+ """Start the callback server in a background thread."""
+ handler_class = self._create_handler_with_data()
+ self.server = HTTPServer(("localhost", self.port), handler_class)
+ self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
+ self.thread.start()
+ print(f"🖥️ Started callback server on http://localhost:{self.port}")
+
+ def stop(self):
+ """Stop the callback server."""
+ if self.server:
+ self.server.shutdown()
+ self.server.server_close()
+ if self.thread:
+ self.thread.join(timeout=1)
+
+ def wait_for_callback(self, timeout=300):
+ """Wait for OAuth callback with timeout."""
+ start_time = time.time()
+ while time.time() - start_time < timeout:
+ if self.callback_data["authorization_code"]:
+ return self.callback_data["authorization_code"]
+ elif self.callback_data["error"]:
+ raise Exception(f"OAuth error: {self.callback_data['error']}")
+ time.sleep(0.1)
+ raise Exception("Timeout waiting for OAuth callback")
+
+ def get_state(self):
+ """Get the received state parameter."""
+ return self.callback_data["state"]
+
+
+class SimpleAuthClient:
+ """Simple MCP client with auth support."""
+
+ def __init__(self, server_url: str, transport_type: str = "streamable_http"):
+ self.server_url = server_url
+ self.transport_type = transport_type
+ self.session: ClientSession | None = None
+
+ async def connect(self):
+ """Connect to the MCP server."""
+ print(f"🔗 Attempting to connect to {self.server_url}...")
+
+ try:
+ callback_server = CallbackServer(port=3030)
+ callback_server.start()
+
+ async def callback_handler() -> tuple[str, str | None]:
+ """Wait for OAuth callback and return auth code and state."""
+ print("⏳ Waiting for authorization callback...")
+ try:
+ auth_code = callback_server.wait_for_callback(timeout=300)
+ return auth_code, callback_server.get_state()
+ finally:
+ callback_server.stop()
+
+ client_metadata_dict = {
+ "client_name": "Simple Auth Client",
+ "redirect_uris": ["http://localhost:3030/callback"],
+ "grant_types": ["authorization_code", "refresh_token"],
+ "response_types": ["code"],
+ "token_endpoint_auth_method": "client_secret_post",
+ }
+
+ async def _default_redirect_handler(authorization_url: str) -> None:
+ """Default redirect handler that opens the URL in a browser."""
+ print(f"Opening browser for authorization: {authorization_url}")
+ webbrowser.open(authorization_url)
+
+ # Create OAuth authentication handler using the new interface
+ oauth_auth = OAuthClientProvider(
+ server_url=self.server_url.replace("/mcp", ""),
+ client_metadata=OAuthClientMetadata.model_validate(client_metadata_dict),
+ storage=InMemoryTokenStorage(),
+ redirect_handler=_default_redirect_handler,
+ callback_handler=callback_handler,
+ )
+
+ # Create transport with auth handler based on transport type
+ if self.transport_type == "sse":
+ print("📡 Opening SSE transport connection with auth...")
+ async with sse_client(
+ url=self.server_url,
+ auth=oauth_auth,
+ timeout=60,
+ ) as (read_stream, write_stream):
+ await self._run_session(read_stream, write_stream, None)
+ else:
+ print("📡 Opening StreamableHTTP transport connection with auth...")
+ async with streamablehttp_client(
+ url=self.server_url,
+ auth=oauth_auth,
+ timeout=timedelta(seconds=60),
+ ) as (read_stream, write_stream, get_session_id):
+ await self._run_session(read_stream, write_stream, get_session_id)
+
+ except Exception as e:
+ print(f"❌ Failed to connect: {e}")
+ import traceback
+
+ traceback.print_exc()
+
+ async def _run_session(self, read_stream, write_stream, get_session_id):
+ """Run the MCP session with the given streams."""
+ print("🤝 Initializing MCP session...")
+ async with ClientSession(read_stream, write_stream) as session:
+ self.session = session
+ print("⚡ Starting session initialization...")
+ await session.initialize()
+ print("✨ Session initialization complete!")
+
+ print(f"\n✅ Connected to MCP server at {self.server_url}")
+ if get_session_id:
+ session_id = get_session_id()
+ if session_id:
+ print(f"Session ID: {session_id}")
+
+ # Run interactive loop
+ await self.interactive_loop()
+
+ async def list_tools(self):
+ """List available tools from the server."""
+ if not self.session:
+ print("❌ Not connected to server")
+ return
+
+ try:
+ result = await self.session.list_tools()
+ if hasattr(result, "tools") and result.tools:
+ print("\n📋 Available tools:")
+ for i, tool in enumerate(result.tools, 1):
+ print(f"{i}. {tool.name}")
+ if tool.description:
+ print(f" Description: {tool.description}")
+ print()
+ else:
+ print("No tools available")
+ except Exception as e:
+ print(f"❌ Failed to list tools: {e}")
+
+ async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None = None):
+ """Call a specific tool."""
+ if not self.session:
+ print("❌ Not connected to server")
+ return
+
+ try:
+ result = await self.session.call_tool(tool_name, arguments or {})
+ print(f"\n🔧 Tool '{tool_name}' result:")
+ if hasattr(result, "content"):
+ for content in result.content:
+ if content.type == "text":
+ print(content.text)
+ else:
+ print(content)
+ else:
+ print(result)
+ except Exception as e:
+ print(f"❌ Failed to call tool '{tool_name}': {e}")
+
+ async def interactive_loop(self):
+ """Run interactive command loop."""
+ print("\n🎯 Interactive MCP Client")
+ print("Commands:")
+ print(" list - List available tools")
+ print(" call <tool_name> [args] - Call a tool")
+ print(" quit - Exit the client")
+ print()
+
+ while True:
+ try:
+ command = input("mcp> ").strip()
+
+ if not command:
+ continue
+
+ if command == "quit":
+ break
+
+ elif command == "list":
+ await self.list_tools()
+
+ elif command.startswith("call "):
+ parts = command.split(maxsplit=2)
+ tool_name = parts[1] if len(parts) > 1 else ""
+
+ if not tool_name:
+ print("❌ Please specify a tool name")
+ continue
+
+ # Parse arguments (simple JSON-like format)
+ arguments = {}
+ if len(parts) > 2:
+ import json
+
+ try:
+ arguments = json.loads(parts[2])
+ except json.JSONDecodeError:
+ print("❌ Invalid arguments format (expected JSON)")
+ continue
+
+ await self.call_tool(tool_name, arguments)
+
+ else:
+ print("❌ Unknown command. Try 'list', 'call <tool_name>', or 'quit'")
+
+ except KeyboardInterrupt:
+ print("\n\n👋 Goodbye!")
+ break
+ except EOFError:
+ break
+
+
+async def main():
+ """Main entry point."""
+ # Default server URL - can be overridden with environment variable
+ # Most MCP streamable HTTP servers use /mcp as the endpoint
+ server_url = os.getenv("MCP_SERVER_PORT", 8000)
+ transport_type = os.getenv("MCP_TRANSPORT_TYPE", "streamable_http")
+ server_url = f"http://localhost:{server_url}/mcp" if transport_type == "streamable_http" else f"http://localhost:{server_url}/sse"
+
+ print("🚀 Simple MCP Auth Client")
+ print(f"Connecting to: {server_url}")
+ print(f"Transport type: {transport_type}")
+
+ # Start connection flow - OAuth will be handled automatically
+ client = SimpleAuthClient(server_url, transport_type)
+ await client.connect()
+
+
+def cli():
+ """CLI entry point for uv script."""
+ asyncio.run(main())
+
+
+if __name__ == "__main__":
+ cli()
diff --git a/poetry.lock b/poetry.lock
index 4c6a02aaf..049f04f22 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -920,7 +920,7 @@ description = "Extensible memoizing collections and decorators"
optional = true
python-versions = ">=3.7"
groups = ["main"]
-markers = "extra == \"google\""
+markers = "extra == \"google\" or extra == \"experimental\" or extra == \"all\""
files = [
{file = "cachetools-5.5.2-py3-none-any.whl", hash = "sha256:d26a22bcc62eb95c3beabd9f1ee5e820d3d2704fe2967cbe350e20c8ffcd3f0a"},
{file = "cachetools-5.5.2.tar.gz", hash = "sha256:1a661caa9175d26759571b2e19580f9d6393969e5dfca11fdb1f947a23e640d4"},
@@ -2318,6 +2318,55 @@ benchmarks = ["httplib2", "httpx", "requests", "urllib3"]
dev = ["dpkt", "pytest", "requests"]
examples = ["oauth2"]
+[[package]]
+name = "google-api-core"
+version = "2.25.1"
+description = "Google API client core library"
+optional = true
+python-versions = ">=3.7"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "google_api_core-2.25.1-py3-none-any.whl", hash = "sha256:8a2a56c1fef82987a524371f99f3bd0143702fecc670c72e600c1cda6bf8dbb7"},
+ {file = "google_api_core-2.25.1.tar.gz", hash = "sha256:d2aaa0b13c78c61cb3f4282c464c046e45fbd75755683c9c525e6e8f7ed0a5e8"},
+]
+
+[package.dependencies]
+google-auth = ">=2.14.1,<3.0.0"
+googleapis-common-protos = ">=1.56.2,<2.0.0"
+proto-plus = [
+ {version = ">=1.22.3,<2.0.0"},
+ {version = ">=1.25.0,<2.0.0", markers = "python_version >= \"3.13\""},
+]
+protobuf = ">=3.19.5,<3.20.0 || >3.20.0,<3.20.1 || >3.20.1,<4.21.0 || >4.21.0,<4.21.1 || >4.21.1,<4.21.2 || >4.21.2,<4.21.3 || >4.21.3,<4.21.4 || >4.21.4,<4.21.5 || >4.21.5,<7.0.0"
+requests = ">=2.18.0,<3.0.0"
+
+[package.extras]
+async-rest = ["google-auth[aiohttp] (>=2.35.0,<3.0.0)"]
+grpc = ["grpcio (>=1.33.2,<2.0.0)", "grpcio (>=1.49.1,<2.0.0) ; python_version >= \"3.11\"", "grpcio-status (>=1.33.2,<2.0.0)", "grpcio-status (>=1.49.1,<2.0.0) ; python_version >= \"3.11\""]
+grpcgcp = ["grpcio-gcp (>=0.2.2,<1.0.0)"]
+grpcio-gcp = ["grpcio-gcp (>=0.2.2,<1.0.0)"]
+
+[[package]]
+name = "google-api-python-client"
+version = "2.177.0"
+description = "Google API Client Library for Python"
+optional = true
+python-versions = ">=3.7"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "google_api_python_client-2.177.0-py3-none-any.whl", hash = "sha256:f2f50f11105ab883eb9b6cf38ec54ea5fd4b429249f76444bec90deba5be79b3"},
+ {file = "google_api_python_client-2.177.0.tar.gz", hash = "sha256:9ffd2b57d68f5afa7e6ac64e2c440534eaa056cbb394812a62ff94723c31b50e"},
+]
+
+[package.dependencies]
+google-api-core = ">=1.31.5,<2.0.dev0 || >2.3.0,<3.0.0"
+google-auth = ">=1.32.0,<2.24.0 || >2.24.0,<2.25.0 || >2.25.0,<3.0.0"
+google-auth-httplib2 = ">=0.2.0,<1.0.0"
+httplib2 = ">=0.19.0,<1.0.0"
+uritemplate = ">=3.0.1,<5"
+
[[package]]
name = "google-auth"
version = "2.40.3"
@@ -2325,7 +2374,7 @@ description = "Google Authentication Library"
optional = true
python-versions = ">=3.7"
groups = ["main"]
-markers = "extra == \"google\""
+markers = "extra == \"google\" or extra == \"experimental\" or extra == \"all\""
files = [
{file = "google_auth-2.40.3-py2.py3-none-any.whl", hash = "sha256:1370d4593e86213563547f97a92752fc658456fe4514c809544f330fed45a7ca"},
{file = "google_auth-2.40.3.tar.gz", hash = "sha256:500c3a29adedeb36ea9cf24b8d10858e152f2412e3ca37829b3fa18e33d63b77"},
@@ -2346,6 +2395,42 @@ requests = ["requests (>=2.20.0,<3.0.0)"]
testing = ["aiohttp (<3.10.0)", "aiohttp (>=3.6.2,<4.0.0)", "aioresponses", "cryptography (<39.0.0) ; python_version < \"3.8\"", "cryptography (>=38.0.3)", "flask", "freezegun", "grpcio", "mock", "oauth2client", "packaging", "pyjwt (>=2.0)", "pyopenssl (<24.3.0)", "pyopenssl (>=20.0.0)", "pytest", "pytest-asyncio", "pytest-cov", "pytest-localserver", "pyu2f (>=0.1.5)", "requests (>=2.20.0,<3.0.0)", "responses", "urllib3"]
urllib3 = ["packaging", "urllib3"]
+[[package]]
+name = "google-auth-httplib2"
+version = "0.2.0"
+description = "Google Authentication Library: httplib2 transport"
+optional = true
+python-versions = "*"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "google-auth-httplib2-0.2.0.tar.gz", hash = "sha256:38aa7badf48f974f1eb9861794e9c0cb2a0511a4ec0679b1f886d108f5640e05"},
+ {file = "google_auth_httplib2-0.2.0-py2.py3-none-any.whl", hash = "sha256:b65a0a2123300dd71281a7bf6e64d65a0759287df52729bdd1ae2e47dc311a3d"},
+]
+
+[package.dependencies]
+google-auth = "*"
+httplib2 = ">=0.19.0"
+
+[[package]]
+name = "google-cloud-profiler"
+version = "4.1.0"
+description = "Google Cloud Profiler Python Agent"
+optional = true
+python-versions = "*"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "google-cloud-profiler-4.1.0.tar.gz", hash = "sha256:2d90f9c6d4c075ad6d43752ae39424c3d0bad63e6549a2c761881f9a235067ae"},
+]
+
+[package.dependencies]
+google-api-python-client = "<1.12.0 || >1.12.0,<2.0.2 || >2.0.2"
+google-auth = ">=1.0.0"
+google-auth-httplib2 = "*"
+protobuf = ">=3.20"
+requests = "*"
+
[[package]]
name = "google-genai"
version = "1.25.0"
@@ -2775,6 +2860,22 @@ http2 = ["h2 (>=3,<5)"]
socks = ["socksio (==1.*)"]
trio = ["trio (>=0.22.0,<1.0)"]
+[[package]]
+name = "httplib2"
+version = "0.22.0"
+description = "A comprehensive HTTP client library."
+optional = true
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "httplib2-0.22.0-py3-none-any.whl", hash = "sha256:14ae0a53c1ba8f3d37e9e27cf37eabb0fb9980f435ba405d546948b009dd64dc"},
+ {file = "httplib2-0.22.0.tar.gz", hash = "sha256:d7a10bc5ef5ab08322488bde8c726eeee5c8618723fdb399597ec58f3d82df81"},
+]
+
+[package.dependencies]
+pyparsing = {version = ">=2.4.2,<3.0.0 || >3.0.0,<3.0.1 || >3.0.1,<3.0.2 || >3.0.2,<3.0.3 || >3.0.3,<4", markers = "python_version > \"3.0\""}
+
[[package]]
name = "httpx"
version = "0.28.1"
@@ -3545,14 +3646,14 @@ pytest = ["pytest (>=7.0.0)", "rich (>=13.9.4,<14.0.0)"]
[[package]]
name = "letta-client"
-version = "0.1.219"
+version = "0.1.220"
description = ""
optional = false
python-versions = "<4.0,>=3.8"
groups = ["main"]
files = [
- {file = "letta_client-0.1.219-py3-none-any.whl", hash = "sha256:f5ad81163f60f5750fce1422e66df318be34f8731d8eeb427750c6dc7d7e698d"},
- {file = "letta_client-0.1.219.tar.gz", hash = "sha256:eb75ac70222770dc185f3b7ed9982c168aabc3993836f8c92e1b835317714781"},
+ {file = "letta_client-0.1.220-py3-none-any.whl", hash = "sha256:ba5faaadc2f8819d846ca2381dc3d757525f6af97822fba31f0643edd521eeef"},
+ {file = "letta_client-0.1.220.tar.gz", hash = "sha256:cebc7e31cae7887d7aa684803a6c4679fa9e1ecdc1e1572f1b23934969f69523"},
]
[package.dependencies]
@@ -5709,6 +5810,25 @@ files = [
{file = "propcache-0.3.2.tar.gz", hash = "sha256:20d7d62e4e7ef05f221e0db2856b979540686342e7dd9973b815599c7057e168"},
]
+[[package]]
+name = "proto-plus"
+version = "1.26.1"
+description = "Beautiful, Pythonic protocol buffers"
+optional = true
+python-versions = ">=3.7"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "proto_plus-1.26.1-py3-none-any.whl", hash = "sha256:13285478c2dcf2abb829db158e1047e2f1e8d63a077d94263c2b88b043c75a66"},
+ {file = "proto_plus-1.26.1.tar.gz", hash = "sha256:21a515a4c4c0088a773899e23c7bbade3d18f9c66c73edd4c7ee3816bc96a012"},
+]
+
+[package.dependencies]
+protobuf = ">=3.19.0,<7.0.0"
+
+[package.extras]
+testing = ["google-api-core (>=1.31.5)"]
+
[[package]]
name = "protobuf"
version = "5.29.5"
@@ -5891,7 +6011,7 @@ description = "Pure-Python implementation of ASN.1 types and DER/BER/CER codecs
optional = true
python-versions = ">=3.8"
groups = ["main"]
-markers = "extra == \"google\""
+markers = "extra == \"google\" or extra == \"experimental\" or extra == \"all\""
files = [
{file = "pyasn1-0.6.1-py3-none-any.whl", hash = "sha256:0d632f46f2ba09143da3a8afe9e33fb6f92fa2320ab7e886e2d0f7672af84629"},
{file = "pyasn1-0.6.1.tar.gz", hash = "sha256:6f580d2bdd84365380830acf45550f2511469f673cb4a5ae3857a3170128b034"},
@@ -5904,7 +6024,7 @@ description = "A collection of ASN.1-based protocols modules"
optional = true
python-versions = ">=3.8"
groups = ["main"]
-markers = "extra == \"google\""
+markers = "extra == \"google\" or extra == \"experimental\" or extra == \"all\""
files = [
{file = "pyasn1_modules-0.4.2-py3-none-any.whl", hash = "sha256:29253a9207ce32b64c3ac6600edc75368f98473906e8fd1043bd6b5b1de2c14a"},
{file = "pyasn1_modules-0.4.2.tar.gz", hash = "sha256:677091de870a80aae844b1ca6134f54652fa2c8c5a52aa396440ac3106e941e6"},
@@ -7079,7 +7199,7 @@ description = "Pure-Python RSA implementation"
optional = true
python-versions = "<4,>=3.6"
groups = ["main"]
-markers = "extra == \"google\""
+markers = "extra == \"google\" or extra == \"experimental\" or extra == \"all\""
files = [
{file = "rsa-4.9.1-py3-none-any.whl", hash = "sha256:68635866661c6836b8d39430f97a996acbd61bfa49406748ea243539fe239762"},
{file = "rsa-4.9.1.tar.gz", hash = "sha256:e7bdbfdb5497da4c07dfd35530e1a902659db6ff241e39d9953cad06ebd0ae75"},
@@ -7857,6 +7977,19 @@ tzdata = {version = "*", markers = "platform_system == \"Windows\""}
[package.extras]
devenv = ["check-manifest", "pytest (>=4.3)", "pytest-cov", "pytest-mock (>=3.3)", "zest.releaser"]
+[[package]]
+name = "uritemplate"
+version = "4.2.0"
+description = "Implementation of RFC 6570 URI Templates"
+optional = true
+python-versions = ">=3.9"
+groups = ["main"]
+markers = "extra == \"experimental\" or extra == \"all\""
+files = [
+ {file = "uritemplate-4.2.0-py3-none-any.whl", hash = "sha256:962201ba1c4edcab02e60f9a0d3821e82dfc5d2d6662a21abd533879bdb8a686"},
+ {file = "uritemplate-4.2.0.tar.gz", hash = "sha256:480c2ed180878955863323eea31b0ede668795de182617fef9c6ca09e6ec9d0e"},
+]
+
[[package]]
name = "urllib3"
version = "2.5.0"
@@ -8681,12 +8814,12 @@ cffi = {version = ">=1.11", markers = "platform_python_implementation == \"PyPy\
cffi = ["cffi (>=1.11)"]
[extras]
-all = ["autoflake", "black", "docker", "fastapi", "granian", "isort", "langchain", "langchain-community", "locust", "pexpect", "pg8000", "pgvector", "pinecone", "pre-commit", "psycopg2", "psycopg2-binary", "pyright", "pytest-asyncio", "pytest-order", "redis", "uvicorn", "uvloop", "wikipedia"]
+all = ["autoflake", "black", "docker", "fastapi", "google-cloud-profiler", "granian", "isort", "langchain", "langchain-community", "locust", "pexpect", "pg8000", "pgvector", "pinecone", "pre-commit", "psycopg2", "psycopg2-binary", "pyright", "pytest-asyncio", "pytest-order", "redis", "uvicorn", "uvloop", "wikipedia"]
bedrock = ["aioboto3", "boto3"]
cloud-tool-sandbox = ["e2b-code-interpreter"]
desktop = ["docker", "fastapi", "langchain", "langchain-community", "locust", "pyright", "uvicorn", "wikipedia"]
dev = ["autoflake", "black", "isort", "locust", "pexpect", "pre-commit", "pyright", "pytest-asyncio", "pytest-order"]
-experimental = ["granian", "uvloop"]
+experimental = ["google-cloud-profiler", "granian", "uvloop"]
external-tools = ["docker", "firecrawl-py", "langchain", "langchain-community", "wikipedia"]
google = ["google-genai"]
pinecone = ["pinecone"]
@@ -8698,4 +8831,4 @@ tests = ["wikipedia"]
[metadata]
lock-version = "2.1"
python-versions = "<3.14,>=3.10"
-content-hash = "278abdf8da256eed6451ab7a667b7f039773bb48c042b90983b82399b902ed03"
+content-hash = "aed0b8f2e1904d30a694331d6e2c428a097433ed2169253426b38580826b8fc7"
diff --git a/pyproject.toml b/pyproject.toml
index ab4f6479d..614fc7bdc 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "letta"
-version = "0.9.0"
+version = "0.9.1"
packages = [
{include = "letta"},
]
@@ -72,7 +72,7 @@ llama-index = "^0.12.2"
llama-index-embeddings-openai = "^0.3.1"
e2b-code-interpreter = {version = "^1.0.3", optional = true}
anthropic = "^0.49.0"
-letta_client = "^0.1.219"
+letta_client = "^0.1.220"
openai = "^1.60.0"
opentelemetry-api = "1.30.0"
opentelemetry-sdk = "1.30.0"
@@ -101,6 +101,7 @@ certifi = "^2025.6.15"
aioboto3 = {version = "^14.3.0", optional = true}
pinecone = {extras = ["asyncio"], version = "^7.3.0", optional = true}
markitdown = {extras = ["docx", "pdf", "pptx"], version = "^0.1.2"}
+google-cloud-profiler = {version = "^4.1.0", optional = true}
[tool.poetry.extras]
@@ -108,7 +109,7 @@ postgres = ["pgvector", "pg8000", "psycopg2-binary", "psycopg2", "asyncpg"]
redis = ["redis"]
pinecone = ["pinecone"]
dev = ["pytest", "pytest-asyncio", "pexpect", "black", "pre-commit", "pyright", "pytest-order", "autoflake", "isort", "locust"]
-experimental = ["uvloop", "granian"]
+experimental = ["uvloop", "granian", "google-cloud-profiler"]
server = ["websockets", "fastapi", "uvicorn"]
cloud-tool-sandbox = ["e2b-code-interpreter"]
external-tools = ["docker", "langchain", "wikipedia", "langchain-community", "firecrawl-py"]
@@ -116,7 +117,7 @@ tests = ["wikipedia"]
bedrock = ["boto3", "aioboto3"]
google = ["google-genai"]
desktop = ["pyright", "websockets", "fastapi", "uvicorn", "docker", "langchain", "wikipedia", "langchain-community", "locust"]
-all = ["pgvector", "pg8000", "psycopg2-binary", "psycopg2", "pytest", "pytest-asyncio", "pexpect", "black", "pre-commit", "pyright", "pytest-order", "autoflake", "isort", "websockets", "fastapi", "uvicorn", "docker", "langchain", "wikipedia", "langchain-community", "locust", "uvloop", "granian", "redis", "pinecone"]
+all = ["pgvector", "pg8000", "psycopg2-binary", "psycopg2", "pytest", "pytest-asyncio", "pexpect", "black", "pre-commit", "pyright", "pytest-order", "autoflake", "isort", "websockets", "fastapi", "uvicorn", "docker", "langchain", "wikipedia", "langchain-community", "locust", "uvloop", "granian", "redis", "pinecone", "google-cloud-profiler"]
[tool.poetry.group.dev.dependencies]
black = "^24.4.2"
diff --git a/tests/integration_test_async_tool_sandbox.py b/tests/integration_test_async_tool_sandbox.py
index d1f599aae..bd01460e9 100644
--- a/tests/integration_test_async_tool_sandbox.py
+++ b/tests/integration_test_async_tool_sandbox.py
@@ -908,11 +908,12 @@ def test_async_function_detection(add_integers_tool, async_add_integers_tool, te
assert async_sandbox.is_async_function
-def test_async_template_selection(add_integers_tool, async_add_integers_tool, test_user):
+async def test_async_template_selection(add_integers_tool, async_add_integers_tool, test_user):
"""Test that correct templates are selected for sync vs async functions"""
# Test sync function uses regular template
sync_sandbox = AsyncToolSandboxE2B(add_integers_tool.name, {}, test_user, tool_object=add_integers_tool)
- sync_script = sync_sandbox.generate_execution_script(agent_state=None)
+ sync_script = await sync_sandbox.generate_execution_script(agent_state=None)
print("=== SYNC SCRIPT ===")
print(sync_script)
print("=== END SYNC SCRIPT ===")
@@ -921,7 +922,7 @@ def test_async_template_selection(add_integers_tool, async_add_integers_tool, te
# Test async function uses async template
async_sandbox = AsyncToolSandboxE2B(async_add_integers_tool.name, {}, test_user, tool_object=async_add_integers_tool)
- async_script = async_sandbox.generate_execution_script(agent_state=None)
+ async_script = await async_sandbox.generate_execution_script(agent_state=None)
print("=== ASYNC SCRIPT ===")
print(async_script)
print("=== END ASYNC SCRIPT ===")
diff --git a/tests/mcp/mcp_config.json b/tests/mcp/mcp_config.json
index 0967ef424..9e26dfeeb 100644
--- a/tests/mcp/mcp_config.json
+++ b/tests/mcp/mcp_config.json
@@ -1 +1 @@
-{}
+{}
\ No newline at end of file
diff --git a/tests/test_agent_serialization_v2.py b/tests/test_agent_serialization_v2.py
index 5d0b8570f..dd0e3b0d3 100644
--- a/tests/test_agent_serialization_v2.py
+++ b/tests/test_agent_serialization_v2.py
@@ -283,6 +283,68 @@ async def agent_with_files(server: SyncServer, default_user, test_block, weather
return (agent_state.id, test_source.id, test_file.id)
+async def test_mcp_server(server: SyncServer, default_user):
+ """Fixture to create and return a test MCP server."""
+ from letta.schemas.mcp import MCPServer, MCPServerType
+
+ mcp_server_data = MCPServer(
+ server_name="test_mcp_server",
+ server_type=MCPServerType.SSE,
+ server_url="http://test-mcp-server.com",
+ token="test-token-12345", # This should be excluded during export
+ custom_headers={"X-API-Key": "secret-key"}, # This should be excluded during export
+ )
+ mcp_server = await server.mcp_manager.create_or_update_mcp_server(mcp_server_data, default_user)
+ yield mcp_server
+
+
+async def mcp_tool(server: SyncServer, default_user, test_mcp_server):
+ """Fixture to create and return an MCP tool."""
+ from letta.schemas.tool import MCPTool, ToolCreate
+
+ # Create a mock MCP tool
+ mcp_tool_data = MCPTool(
+ name="test_mcp_tool",
+ description="Test MCP tool for serialization",
+ inputSchema={"type": "object", "properties": {"input": {"type": "string"}}},
+ )
+ tool_create = ToolCreate.from_mcp(test_mcp_server.server_name, mcp_tool_data)
+
+ # Create tool with MCP metadata
+ mcp_tool = await server.tool_manager.create_mcp_tool_async(tool_create, test_mcp_server.server_name, test_mcp_server.id, default_user)
+ yield mcp_tool
+
+
+async def agent_with_mcp_tools(server: SyncServer, default_user, test_block, mcp_tool, test_mcp_server):
+ """Fixture to create and return an agent with MCP tools."""
+ memory_blocks = [
+ CreateBlock(label="human", value="User is a test user"),
+ CreateBlock(label="persona", value="I am a helpful test assistant"),
+ ]
+
+ create_agent_request = CreateAgent(
+ name="test_agent_mcp",
+ system="You are a helpful assistant with MCP tools.",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[test_block.id],
+ tool_ids=[mcp_tool.id],
+ tags=["test", "mcp", "export"],
+ description="Test agent with MCP tools for serialization testing",
+ )
+
+ agent_state = await server.agent_manager.create_agent_async(
+ agent_create=create_agent_request,
+ actor=default_user,
+ )
+
+ return agent_state
+
+
# ------------------------------
# Helper Functions
# ------------------------------
@@ -1030,6 +1092,7 @@ class TestAgentFileImport:
files=[],
sources=[],
tools=[],
+ mcp_servers=[],
)
with pytest.raises(AgentFileImportError):
@@ -1258,7 +1321,7 @@ class TestAgentFileValidation:
files=[],
sources=[],
tools=[],
- # mcp_servers=[],
+ mcp_servers=[],
)
# Should not raise
@@ -1302,7 +1365,7 @@ class TestAgentFileValidation:
json_schema={"name": "test_tool", "parameters": {"type": "object", "properties": {}}},
)
],
- # mcp_servers=[],
+ mcp_servers=[],
)
assert validate_id_format(valid_schema)
@@ -1316,11 +1379,282 @@ class TestAgentFileValidation:
files=[],
sources=[],
tools=[],
- # mcp_servers=[],
+ mcp_servers=[],
)
assert not validate_id_format(invalid_schema)
+class TestMCPServerSerialization:
+ """Tests for MCP server export/import functionality."""
+
+ async def test_mcp_server_export(self, agent_serialization_manager, agent_with_mcp_tools, default_user):
+ """Test that MCP servers are exported correctly."""
+ agent_file = await agent_serialization_manager.export([agent_with_mcp_tools.id], default_user)
+
+ # Verify MCP server is included
+ assert len(agent_file.mcp_servers) == 1
+ mcp_server = agent_file.mcp_servers[0]
+
+ # Verify server details
+ assert mcp_server.server_name == "test_mcp_server"
+ assert mcp_server.server_url == "http://test-mcp-server.com"
+ assert mcp_server.server_type == "sse"
+
+ # Verify auth fields are excluded
+ assert not hasattr(mcp_server, "token")
+ assert not hasattr(mcp_server, "custom_headers")
+
+ # Verify ID format
+ assert _validate_entity_id(mcp_server.id, "mcp_server")
+
+ async def test_mcp_server_auth_scrubbing(self, server, agent_serialization_manager, default_user):
+ """Test that authentication information is scrubbed during export."""
+ from letta.schemas.mcp import MCPServer, MCPServerType
+
+ # Create MCP server with auth info
+ mcp_server_data_stdio = MCPServer(
+ server_name="auth_test_server",
+ server_type=MCPServerType.STDIO,
+ # token="super-secret-token",
+ # custom_headers={"Authorization": "Bearer secret-key", "X-Custom": "custom-value"},
+ stdio_config={
+ "server_name": "auth_test_server",
+ "command": "test-command",
+ "args": ["arg1", "arg2"],
+ "env": {"ENV_VAR": "value"},
+ },
+ )
+ mcp_server = await server.mcp_manager.create_or_update_mcp_server(mcp_server_data_stdio, default_user)
+
+ mcp_server_data_http = MCPServer(
+ server_name="auth_test_server_http",
+ server_type=MCPServerType.STREAMABLE_HTTP,
+ server_url="http://auth_test_server_http.com",
+ token="super-secret-token",
+ custom_headers={"X-Custom": "custom-value"},
+ )
+ mcp_server_http = await server.mcp_manager.create_or_update_mcp_server(mcp_server_data_http, default_user)
+ # Create tool from MCP server
+ from letta.schemas.tool import MCPTool, ToolCreate
+
+ mcp_tool_data = MCPTool(
+ name="auth_test_tool_stdio",
+ description="Tool with auth",
+ inputSchema={"type": "object", "properties": {}},
+ )
+ tool_create_stdio = ToolCreate.from_mcp(mcp_server.server_name, mcp_tool_data)
+
+ mcp_tool_data_http = MCPTool(
+ name="auth_test_tool_http",
+ description="Tool with auth",
+ inputSchema={"type": "object", "properties": {}},
+ )
+
+ tool_create_http = ToolCreate.from_mcp(mcp_server_http.server_name, mcp_tool_data_http)
+
+ mcp_tool = await server.tool_manager.create_mcp_tool_async(tool_create_stdio, mcp_server.server_name, mcp_server.id, default_user)
+ mcp_tool_http = await server.tool_manager.create_mcp_tool_async(
+ tool_create_http, mcp_server_http.server_name, mcp_server_http.id, default_user
+ )
+
+ # Create agent with the tool
+ from letta.schemas.agent import CreateAgent
+
+ create_agent_request = CreateAgent(
+ name="auth_test_agent",
+ tool_ids=[mcp_tool.id, mcp_tool_http.id],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ )
+ agent = await server.agent_manager.create_agent_async(create_agent_request, default_user)
+
+ # Export
+ agent_file = await agent_serialization_manager.export([agent.id], default_user)
+
+ for server in agent_file.mcp_servers:
+ if server.server_name == "auth_test_server":
+ exported_server_stdio = server
+ elif server.server_name == "auth_test_server_http":
+ exported_server_http = server
+
+ # Verify env variables in stdio server are excluded (typically used for auth)
+ assert exported_server_stdio.id != mcp_server.id
+ assert exported_server_stdio.server_name == "auth_test_server"
+ assert exported_server_stdio.stdio_config == {
+ "server_name": "auth_test_server",
+ "type": "stdio",
+ "command": "test-command",
+ "args": ["arg1", "arg2"],
+ } # Non-auth config preserved
+ assert exported_server_stdio.server_type == "stdio"
+
+ # Verify token and custom headers are excluded from export for http server
+ assert exported_server_http.id != mcp_server_http.id
+ assert exported_server_http.server_name == "auth_test_server_http"
+ assert exported_server_http.server_type == "streamable_http"
+ assert exported_server_http.server_url == "http://auth_test_server_http.com"
+ assert not hasattr(exported_server_http, "token")
+ assert not hasattr(exported_server_http, "custom_headers")
+
+ async def test_mcp_tool_metadata_with_server_id(self, agent_serialization_manager, agent_with_mcp_tools, default_user):
+ """Test that MCP tools have server_id in metadata."""
+ agent_file = await agent_serialization_manager.export([agent_with_mcp_tools.id], default_user)
+
+ # Find the MCP tool
+ mcp_tool = next((t for t in agent_file.tools if t.name == "test_mcp_tool"), None)
+ assert mcp_tool is not None
+
+ # Verify metadata contains server info
+ assert mcp_tool.metadata_ is not None
+ assert "mcp" in mcp_tool.metadata_
+ assert "server_name" in mcp_tool.metadata_["mcp"]
+ assert "server_id" in mcp_tool.metadata_["mcp"]
+ assert mcp_tool.metadata_["mcp"]["server_name"] == "test_mcp_server"
+
+ # Verify tag format
+ assert any(tag.startswith("mcp:") for tag in mcp_tool.tags)
+
+ async def test_mcp_server_import(self, agent_serialization_manager, agent_with_mcp_tools, default_user, other_user):
+ """Test importing agents with MCP servers."""
+ # Export from default user
+ agent_file = await agent_serialization_manager.export([agent_with_mcp_tools.id], default_user)
+
+ # Import to other user
+ result = await agent_serialization_manager.import_file(agent_file, other_user)
+
+ assert result.success
+
+ # Verify MCP server was imported
+ mcp_server_id = next((db_id for file_id, db_id in result.id_mappings.items() if file_id.startswith("mcp_server-")), None)
+ assert mcp_server_id is not None
+
+ async def test_multiple_mcp_servers_export(self, server, agent_serialization_manager, default_user):
+ """Test exporting multiple MCP servers from different agents."""
+ from letta.schemas.mcp import MCPServer, MCPServerType
+
+ # Create two MCP servers
+ mcp_server1 = await server.mcp_manager.create_or_update_mcp_server(
+ MCPServer(
+ server_name="mcp1",
+ server_type=MCPServerType.STREAMABLE_HTTP,
+ server_url="http://mcp1.com",
+ token="super-secret-token",
+ custom_headers={"X-Custom": "custom-value"},
+ ),
+ default_user,
+ )
+ mcp_server2 = await server.mcp_manager.create_or_update_mcp_server(
+ MCPServer(
+ server_name="mcp2",
+ server_type=MCPServerType.STDIO,
+ stdio_config={
+ "server_name": "mcp2",
+ "command": "mcp2-cmd",
+ "args": ["arg1", "arg2"],
+ },
+ ),
+ default_user,
+ )
+
+ # Create tools from each server
+ from letta.schemas.tool import MCPTool, ToolCreate
+
+ tool1 = await server.tool_manager.create_mcp_tool_async(
+ ToolCreate.from_mcp(
+ "mcp1",
+ MCPTool(name="tool1", description="Tool 1", inputSchema={"type": "object", "properties": {}}),
+ ),
+ "mcp1",
+ mcp_server1.id,
+ default_user,
+ )
+ tool2 = await server.tool_manager.create_mcp_tool_async(
+ ToolCreate.from_mcp(
+ "mcp2",
+ MCPTool(name="tool2", description="Tool 2", inputSchema={"type": "object", "properties": {}}),
+ ),
+ "mcp2",
+ mcp_server2.id,
+ default_user,
+ )
+
+ # Create agents with different MCP tools
+ from letta.schemas.agent import CreateAgent
+
+ agent1 = await server.agent_manager.create_agent_async(
+ CreateAgent(
+ name="agent1",
+ tool_ids=[tool1.id],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ ),
+ default_user,
+ )
+ agent2 = await server.agent_manager.create_agent_async(
+ CreateAgent(
+ name="agent2",
+ tool_ids=[tool2.id],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ ),
+ default_user,
+ )
+
+ # Export both agents
+ agent_file = await agent_serialization_manager.export([agent1.id, agent2.id], default_user)
+
+ # Verify both MCP servers are included
+ assert len(agent_file.mcp_servers) == 2
+
+ # Verify server types
+ streamable_http_server = next(s for s in agent_file.mcp_servers if s.server_name == "mcp1")
+ stdio_server = next(s for s in agent_file.mcp_servers if s.server_name == "mcp2")
+
+ assert streamable_http_server.server_name == "mcp1"
+ assert streamable_http_server.server_type == "streamable_http"
+ assert streamable_http_server.server_url == "http://mcp1.com"
+
+ assert stdio_server.server_name == "mcp2"
+ assert stdio_server.server_type == "stdio"
+ assert stdio_server.stdio_config == {
+ "server_name": "mcp2",
+ "type": "stdio",
+ "command": "mcp2-cmd",
+ "args": ["arg1", "arg2"],
+ }
+
+ async def test_mcp_server_deduplication(self, server, agent_serialization_manager, default_user, test_mcp_server, mcp_tool):
+ """Test that shared MCP servers are deduplicated during export."""
+ # Create two agents using the same MCP tool
+ from letta.schemas.agent import CreateAgent
+
+ agent1 = await server.agent_manager.create_agent_async(
+ CreateAgent(
+ name="agent_dup1",
+ tool_ids=[mcp_tool.id],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ ),
+ default_user,
+ )
+ agent2 = await server.agent_manager.create_agent_async(
+ CreateAgent(
+ name="agent_dup2",
+ tool_ids=[mcp_tool.id],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ ),
+ default_user,
+ )
+
+ # Export both agents
+ agent_file = await agent_serialization_manager.export([agent1.id, agent2.id], default_user)
+
+ # Verify only one MCP server is exported
+ assert len(agent_file.mcp_servers) == 1
+ assert agent_file.mcp_servers[0].server_name == "test_mcp_server"
+
+
if __name__ == "__main__":
pytest.main([__file__])
diff --git a/tests/test_client.py b/tests/test_client.py
index 8f6c2a234..2f9d881d5 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -593,13 +593,6 @@ def test_attach_detach_agent_source(client: Letta, agent: AgentState):
# Create a source
source = client.sources.create(
name="test_source",
- embedding_config={ # TODO: change this
- "embedding_endpoint": "https://embeddings.memgpt.ai",
- "embedding_model": "BAAI/bge-large-en-v1.5",
- "embedding_dim": 1024,
- "embedding_chunk_size": 300,
- "embedding_endpoint_type": "hugging-face",
- },
)
initial_sources = client.agents.sources.list(agent_id=agent.id)
assert source.id not in [s.id for s in initial_sources]
@@ -710,7 +703,7 @@ def test_attach_sleeptime_block(client: Letta):
sleeptime_id = [id for id in agent_ids if id != agent.id][0]
# attach a new block
- block = client.blocks.create(label="test", value="test")
+ block = client.blocks.create(label="test", value="test") # , project_id="test")
client.agents.blocks.attach(agent_id=agent.id, block_id=block.id)
# verify block is attached to both agents
@@ -720,5 +713,8 @@ def test_attach_sleeptime_block(client: Letta):
blocks = client.agents.blocks.list(agent_id=sleeptime_id)
assert block.id in [b.id for b in blocks]
+ # blocks = client.blocks.list(project_id="test")
+ # assert block.id in [b.id for b in blocks]
+
# cleanup
client.agents.delete(agent.id)
diff --git a/tests/test_file_processor.py b/tests/test_file_processor.py
index e2448e2e1..15ba50107 100644
--- a/tests/test_file_processor.py
+++ b/tests/test_file_processor.py
@@ -217,3 +217,77 @@ class TestOpenAIEmbedder:
assert passages[2].embedding[:2] == [0.3, 0.3]
assert passages[3].text == "chunk 4"
assert passages[3].embedding[:2] == [0.4, 0.4]
+
+
+class TestFileProcessorWithPinecone:
+ """Test suite for file processor with Pinecone integration"""
+
+ @pytest.mark.asyncio
+ async def test_file_processor_sets_chunks_embedded_zero_with_pinecone(self):
+ """Test that file processor sets total_chunks and chunks_embedded=0 when using Pinecone"""
+ from letta.schemas.enums import FileProcessingStatus
+ from letta.schemas.file import FileMetadata
+ from letta.services.file_processor.embedder.pinecone_embedder import PineconeEmbedder
+ from letta.services.file_processor.file_processor import FileProcessor
+ from letta.services.file_processor.parser.markitdown_parser import MarkitdownFileParser
+
+ # Mock dependencies
+ mock_actor = Mock()
+ mock_actor.organization_id = "test_org"
+
+ # Create real parser
+ file_parser = MarkitdownFileParser()
+
+ # Create file metadata with content
+ mock_file = FileMetadata(
+ file_name="test.txt",
+ source_id="source-87654321",
+ processing_status=FileProcessingStatus.PARSING,
+ total_chunks=0,
+ chunks_embedded=0,
+ content="This is test content that will be chunked.",
+ )
+
+ # Mock only the Pinecone-specific functionality
+ with patch("letta.services.file_processor.embedder.pinecone_embedder.PINECONE_AVAILABLE", True):
+ with patch("letta.services.file_processor.embedder.pinecone_embedder.upsert_file_records_to_pinecone_index") as mock_upsert:
+ # Mock successful Pinecone upsert
+ mock_upsert.return_value = None
+
+ # Create real Pinecone embedder
+ embedder = PineconeEmbedder()
+
+ # Create file processor with Pinecone enabled
+ file_processor = FileProcessor(file_parser=file_parser, embedder=embedder, actor=mock_actor, using_pinecone=True)
+
+ # Track file manager update calls
+ update_calls = []
+
+ async def track_update(*args, **kwargs):
+ update_calls.append(kwargs)
+ return mock_file
+
+ # Mock managers to track calls
+ with patch.object(file_processor.file_manager, "update_file_status", new=track_update):
+ with patch.object(file_processor.passage_manager, "create_many_source_passages_async", new=AsyncMock()):
+ # Process the imported file (which has content)
+ await file_processor.process_imported_file(mock_file, mock_file.source_id)
+
+ # Find the call that sets total_chunks and chunks_embedded
+ chunk_update_call = None
+ for call in update_calls:
+ if "total_chunks" in call and "chunks_embedded" in call:
+ chunk_update_call = call
+ break
+
+ # Verify the correct values were set
+ assert chunk_update_call is not None, "No update_file_status call found with total_chunks and chunks_embedded"
+ assert chunk_update_call["total_chunks"] > 0, "total_chunks should be greater than 0"
+ assert chunk_update_call["chunks_embedded"] == 0, "chunks_embedded should be 0 when using Pinecone"
+
+ # Verify Pinecone upsert was called
+ mock_upsert.assert_called_once()
+ call_args = mock_upsert.call_args
+ assert call_args.kwargs["file_id"] == mock_file.id
+ assert call_args.kwargs["source_id"] == mock_file.source_id
+ assert len(call_args.kwargs["chunks"]) > 0
diff --git a/tests/test_managers.py b/tests/test_managers.py
index bc2b7c576..4b4b0c5f9 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -55,6 +55,7 @@ from letta.schemas.block import BlockUpdate, CreateBlock
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import ActorType, AgentStepStatus, FileProcessingStatus, JobStatus, JobType, MessageRole, ProviderType
from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata
from letta.schemas.file import FileMetadata as PydanticFileMetadata
from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
from letta.schemas.job import BatchJob
@@ -268,8 +269,11 @@ def mcp_tool(server, default_user):
},
)
mcp_server_name = "test"
+ mcp_server_id = "test-server-id" # Mock server ID for testing
tool_create = ToolCreate.from_mcp(mcp_server_name=mcp_server_name, mcp_tool=mcp_tool)
- tool = server.tool_manager.create_or_update_mcp_tool(tool_create=tool_create, mcp_server_name=mcp_server_name, actor=default_user)
+ tool = server.tool_manager.create_or_update_mcp_tool(
+ tool_create=tool_create, mcp_server_name=mcp_server_name, mcp_server_id=mcp_server_id, actor=default_user
+ )
yield tool
@@ -2133,6 +2137,8 @@ async def test_list_agents_query_text_pagination(server: SyncServer, default_use
actor=default_user,
)
+ await asyncio.sleep(0.1)
+
agent2 = await server.agent_manager.create_agent_async(
agent_create=CreateAgent(
name="Search Agent Two",
@@ -2145,6 +2151,8 @@ async def test_list_agents_query_text_pagination(server: SyncServer, default_use
actor=default_user,
)
+ await asyncio.sleep(0.1)
+
agent3 = await server.agent_manager.create_agent_async(
agent_create=CreateAgent(
name="Different Agent",
@@ -3469,6 +3477,7 @@ def test_create_mcp_tool(server: SyncServer, mcp_tool, default_user, default_org
assert mcp_tool.created_by_id == default_user.id
assert mcp_tool.tool_type == ToolType.EXTERNAL_MCP
assert mcp_tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_name"] == "test"
+ assert mcp_tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_id"] == "test-server-id"
# Test should work with both SQLite and PostgreSQL
@@ -6570,14 +6579,14 @@ async def test_file_status_race_condition_prevention(server, default_user, defau
processing_status=FileProcessingStatus.PARSING,
)
- # Simulate race condition: Try to update from PENDING again (stale read)
- # This should fail because the file is already in PARSING
- with pytest.raises(ValueError, match="Invalid state transition.*parsing.*PARSING"):
- await server.file_manager.update_file_status(
- file_id=created.id,
- actor=default_user,
- processing_status=FileProcessingStatus.PARSING,
- )
+ # Simulate race condition: Try to update from PARSING to PARSING again (stale read)
+ # This should now be allowed (same-state transition) to prevent race conditions
+ updated_again = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+ assert updated_again.processing_status == FileProcessingStatus.PARSING
# Move to ERROR
await server.file_manager.update_file_status(
@@ -6681,6 +6690,43 @@ async def test_file_status_update_with_chunks_progress(server, default_user, def
assert updated.chunks_embedded == 100 # preserved
+async def test_same_state_transitions_allowed(server, default_user, default_source):
+ """Test that same-state transitions are allowed to prevent race conditions."""
+ # Create file
+ created = await server.file_manager.create_file(
+ FileMetadata(
+ file_name="same_state_test.txt",
+ source_id=default_source.id,
+ processing_status=FileProcessingStatus.PENDING,
+ ),
+ default_user,
+ )
+
+ # Test PARSING -> PARSING
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING
+ )
+ assert updated.processing_status == FileProcessingStatus.PARSING
+
+ # Test EMBEDDING -> EMBEDDING
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING, chunks_embedded=5
+ )
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING
+ assert updated.chunks_embedded == 5
+
+ # Test COMPLETED -> COMPLETED
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED, total_chunks=10
+ )
+ assert updated.processing_status == FileProcessingStatus.COMPLETED
+ assert updated.total_chunks == 10
+
+
@pytest.mark.asyncio
async def test_upsert_file_content_basic(server: SyncServer, default_user, default_source, async_session):
"""Test creating and updating file content with upsert_file_content()."""
@@ -8510,6 +8556,83 @@ async def test_create_mcp_server(server, default_user, event_loop):
print("TAGS", tool.tags)
+async def test_get_mcp_servers_by_ids(server, default_user, event_loop):
+ from letta.schemas.mcp import MCPServer, MCPServerType, SSEServerConfig, StdioServerConfig
+ from letta.settings import tool_settings
+
+ if tool_settings.mcp_read_from_config:
+ return
+
+ # Create multiple MCP servers for testing
+ servers_data = [
+ {
+ "name": "test_server_1",
+ "config": StdioServerConfig(
+ server_name="test_server_1", type=MCPServerType.STDIO, command="echo 'test1'", args=["arg1"], env={"ENV1": "value1"}
+ ),
+ "type": MCPServerType.STDIO,
+ },
+ {
+ "name": "test_server_2",
+ "config": SSEServerConfig(server_name="test_server_2", server_url="https://test2.example.com/sse"),
+ "type": MCPServerType.SSE,
+ },
+ {
+ "name": "test_server_3",
+ "config": SSEServerConfig(server_name="test_server_3", server_url="https://test3.example.com/mcp"),
+ "type": MCPServerType.STREAMABLE_HTTP,
+ },
+ ]
+
+ created_servers = []
+ for server_data in servers_data:
+ if server_data["type"] == MCPServerType.STDIO:
+ mcp_server = MCPServer(server_name=server_data["name"], server_type=server_data["type"], stdio_config=server_data["config"])
+ else:
+ mcp_server = MCPServer(
+ server_name=server_data["name"], server_type=server_data["type"], server_url=server_data["config"].server_url
+ )
+
+ created = await server.mcp_manager.create_or_update_mcp_server(mcp_server, actor=default_user)
+ created_servers.append(created)
+
+ # Test fetching multiple servers by IDs
+ server_ids = [s.id for s in created_servers]
+ fetched_servers = await server.mcp_manager.get_mcp_servers_by_ids(server_ids, actor=default_user)
+
+ assert len(fetched_servers) == len(created_servers)
+ fetched_ids = {s.id for s in fetched_servers}
+ expected_ids = {s.id for s in created_servers}
+ assert fetched_ids == expected_ids
+
+ # Test fetching subset of servers
+ subset_ids = server_ids[:2]
+ subset_servers = await server.mcp_manager.get_mcp_servers_by_ids(subset_ids, actor=default_user)
+ assert len(subset_servers) == 2
+ assert all(s.id in subset_ids for s in subset_servers)
+
+ # Test fetching with empty list
+ empty_result = await server.mcp_manager.get_mcp_servers_by_ids([], actor=default_user)
+ assert empty_result == []
+
+ # Test fetching with non-existent ID mixed with valid IDs
+ mixed_ids = [server_ids[0], "non-existent-id", server_ids[1]]
+ mixed_result = await server.mcp_manager.get_mcp_servers_by_ids(mixed_ids, actor=default_user)
+ # Should only return the existing servers
+ assert len(mixed_result) == 2
+ assert all(s.id in server_ids for s in mixed_result)
+
+ # Test that servers from different organizations are not returned
+ # This would require creating another user/org, but for now we'll just verify
+ # that the function respects the actor's organization
+ all_servers = await server.mcp_manager.list_mcp_servers(actor=default_user)
+ all_server_ids = [s.id for s in all_servers]
+ bulk_fetched = await server.mcp_manager.get_mcp_servers_by_ids(all_server_ids, actor=default_user)
+
+ # All fetched servers should belong to the same organization
+ assert all(s.organization_id == default_user.organization_id for s in bulk_fetched)
+
+
# ======================================================================================================================
# FileAgent Tests
# ======================================================================================================================
diff --git a/tests/test_sources.py b/tests/test_sources.py
index bfbf6a146..e9315c3b0 100644
--- a/tests/test_sources.py
+++ b/tests/test_sources.py
@@ -2,6 +2,7 @@ import os
import re
import threading
import time
+from datetime import datetime, timedelta
import pytest
from dotenv import load_dotenv
@@ -13,6 +14,7 @@ from letta_client.types import AgentState
from letta.constants import DEFAULT_ORG_ID, FILES_TOOLS
from letta.orm.enums import ToolType
+from letta.schemas.enums import FileProcessingStatus
from letta.schemas.message import MessageCreate
from letta.schemas.user import User
from letta.settings import settings
@@ -277,6 +279,10 @@ def test_attach_existing_files_creates_source_blocks_correctly(disable_pinecone,
# Assert that the expected chunk is in the raw system message
expected_chunk = """<directories>
+<file_limits>
+- current_files_open=1
+- max_files_open=5
+</file_limits>
<directory name="test_source">
<file status="open" name="test_source/test.txt">
<metadata>
@@ -334,6 +340,10 @@ def test_delete_source_removes_source_blocks_correctly(disable_pinecone, client:
raw_system_message = get_raw_system_message(client, agent_state.id)
# Assert that the expected chunk is in the raw system message
expected_chunk = """<directories>
+<file_limits>
+- current_files_open=1
+- max_files_open=5
+</file_limits>
<directory name="test_source">
<file status="open" name="test_source/test.txt">
<metadata>
@@ -987,7 +997,7 @@ def test_agent_open_file(disable_pinecone, client: LettaSDKClient, agent_state:
file_metadata = upload_file_and_wait(client, source.id, file_path)
# Basic test open_file function
- closed_files = client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)
+ closed_files = client.agents.files.open(agent_id=agent_state.id, file_id=file_metadata.id)
assert len(closed_files) == 0
@@ -1004,10 +1014,10 @@ def test_agent_close_file(disable_pinecone, client: LettaSDKClient, agent_state:
file_metadata = upload_file_and_wait(client, source.id, file_path)
# First open the file
- client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)
+ client.agents.files.open(agent_id=agent_state.id, file_id=file_metadata.id)
# Test close_file function
- client.agents.close_file(agent_id=agent_state.id, file_id=file_metadata.id)
+ client.agents.files.close(agent_id=agent_state.id, file_id=file_metadata.id)
# Result can be None or any type based on the signature
# Just verify the function executes without error
@@ -1029,11 +1039,74 @@ def test_agent_close_all_open_files(disable_pinecone, client: LettaSDKClient, ag
file_metadata = upload_file_and_wait(client, source.id, file_path)
file_metadatas.append(file_metadata)
# Open each file
- client.agents.open_file(agent_id=agent_state.id, file_id=file_metadata.id)
+ client.agents.files.open(agent_id=agent_state.id, file_id=file_metadata.id)
# Test close_all_open_files function
- result = client.agents.close_all_open_files(agent_id=agent_state.id)
+ result = client.agents.files.close_all(agent_id=agent_state.id)
# Verify result is a list of strings
assert isinstance(result, list), f"Expected list, got {type(result)}"
assert all(isinstance(item, str) for item in result), "All items in result should be strings"
+
+
+def test_file_processing_timeout(disable_pinecone, client: LettaSDKClient):
+ """Test that files in non-terminal states are moved to error after timeout"""
+ # Create a source
+ source = client.sources.create(name="test_timeout_source", embedding="openai/text-embedding-3-small")
+
+ # Upload a file
+ file_path = "tests/data/test.txt"
+ with open(file_path, "rb") as f:
+ file_metadata = client.sources.files.upload(source_id=source.id, file=f)
+
+ # Get the file ID
+ file_id = file_metadata.id
+
+ # Test the is_terminal_state method directly (this doesn't require server mocking)
+ assert FileProcessingStatus.COMPLETED.is_terminal_state() == True
+ assert FileProcessingStatus.ERROR.is_terminal_state() == True
+ assert FileProcessingStatus.PARSING.is_terminal_state() == False
+ assert FileProcessingStatus.EMBEDDING.is_terminal_state() == False
+ assert FileProcessingStatus.PENDING.is_terminal_state() == False
+
+ # For testing the actual timeout logic, we can check the current file status
+ current_file = client.sources.get_file_metadata(source_id=source.id, file_id=file_id)
+
+ # Convert string status to enum for testing
+ status_enum = FileProcessingStatus(current_file.processing_status)
+
+ # Verify that files in terminal states are not affected by timeout checks
+ if status_enum.is_terminal_state():
+ # This is the expected behavior - files that completed processing shouldn't timeout
+ print(f"File {file_id} is in terminal state: {current_file.processing_status}")
+ assert status_enum in [FileProcessingStatus.COMPLETED, FileProcessingStatus.ERROR]
+ else:
+ # If file is still processing, it should eventually complete or timeout
+ # In a real scenario, we'd wait and check, but for unit tests we just verify the logic exists
+ print(f"File {file_id} is still processing: {current_file.processing_status}")
+ assert status_enum in [FileProcessingStatus.PENDING, FileProcessingStatus.PARSING, FileProcessingStatus.EMBEDDING]
+
+
+def test_file_processing_timeout_logic():
+ """Test the timeout logic directly without server dependencies"""
+ from datetime import timezone
+
+ # Test scenario: file created 35 minutes ago, timeout is 30 minutes
+ old_time = datetime.now(timezone.utc) - timedelta(minutes=35)
+ current_time = datetime.now(timezone.utc)
+ timeout_minutes = 30
+
+ # Calculate timeout threshold
+ timeout_threshold = current_time - timedelta(minutes=timeout_minutes)
+
+ # Verify timeout logic
+ assert old_time < timeout_threshold, "File created 35 minutes ago should be past 30-minute timeout"
+
+ # Test edge case: file created exactly at timeout
+ edge_time = current_time - timedelta(minutes=timeout_minutes)
+ assert not (edge_time < timeout_threshold), "File created exactly at timeout should not trigger timeout"
+
+ # Test recent file
+ recent_time = current_time - timedelta(minutes=10)
+ assert not (recent_time < timeout_threshold), "Recent file should not trigger timeout"
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 23658b843..87cfc127f 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -7,7 +7,7 @@ from letta.functions.ast_parsers import coerce_dict_args_by_annotations, get_fun
from letta.schemas.file import FileMetadata
from letta.services.file_processor.chunker.line_chunker import LineChunker
from letta.services.helpers.agent_manager_helper import safe_format
-from letta.utils import sanitize_filename
+from letta.utils import sanitize_filename, validate_function_response
CORE_MEMORY_VAR = "My core memory is that I like to eat bananas"
VARS_DICT = {"CORE_MEMORY": CORE_MEMORY_VAR}
@@ -555,3 +555,122 @@ async def test_get_latest_alembic_revision_consistency(event_loop):
# They should be identical
assert revision_id1 == revision_id2
+
+
+# ---------------------- validate_function_response TESTS ---------------------- #
+
+
+def test_validate_function_response_string_input():
+ """Test that string inputs are returned unchanged when within limit"""
+ response = validate_function_response("hello world", return_char_limit=100)
+ assert response == "hello world"
+
+
+def test_validate_function_response_none_input():
+ """Test that None inputs are converted to 'None' string"""
+ response = validate_function_response(None, return_char_limit=100)
+ assert response == "None"
+
+
+def test_validate_function_response_dict_input():
+ """Test that dict inputs are JSON serialized"""
+ test_dict = {"key": "value", "number": 42}
+ response = validate_function_response(test_dict, return_char_limit=100)
+ # Response should be valid JSON string
+ import json
+
+ parsed = json.loads(response)
+ assert parsed == test_dict
+
+
+def test_validate_function_response_other_types():
+ """Test that other types are converted to strings"""
+ # Test integer
+ response = validate_function_response(42, return_char_limit=100)
+ assert response == "42"
+
+ # Test list
+ response = validate_function_response([1, 2, 3], return_char_limit=100)
+ assert response == "[1, 2, 3]"
+
+ # Test boolean
+ response = validate_function_response(True, return_char_limit=100)
+ assert response == "True"
+
+
+def test_validate_function_response_strict_mode_string():
+ """Test strict mode allows strings"""
+ response = validate_function_response("test", return_char_limit=100, strict=True)
+ assert response == "test"
+
+
+def test_validate_function_response_strict_mode_none():
+ """Test strict mode allows None"""
+ response = validate_function_response(None, return_char_limit=100, strict=True)
+ assert response == "None"
+
+
+def test_validate_function_response_strict_mode_violation():
+ """Test strict mode raises ValueError for non-string/None types"""
+ with pytest.raises(ValueError, match="Strict mode violation. Function returned type: int"):
+ validate_function_response(42, return_char_limit=100, strict=True)
+
+ with pytest.raises(ValueError, match="Strict mode violation. Function returned type: dict"):
+ validate_function_response({"key": "value"}, return_char_limit=100, strict=True)
+
+
+def test_validate_function_response_truncation():
+ """Test that long responses are truncated when truncate=True"""
+ long_string = "a" * 200
+ response = validate_function_response(long_string, return_char_limit=50, truncate=True)
+ assert len(response) > 50 # Should include truncation message
+ assert response.startswith("a" * 50)
+ assert "NOTE: function output was truncated" in response
+ assert "200 > 50" in response
+
+
+def test_validate_function_response_no_truncation():
+ """Test that long responses are not truncated when truncate=False"""
+ long_string = "a" * 200
+ response = validate_function_response(long_string, return_char_limit=50, truncate=False)
+ assert response == long_string
+ assert len(response) == 200
+
+
+def test_validate_function_response_exact_limit():
+ """Test response exactly at the character limit"""
+ exact_string = "a" * 50
+ response = validate_function_response(exact_string, return_char_limit=50, truncate=True)
+ assert response == exact_string
+
+
+def test_validate_function_response_complex_dict():
+ """Test with complex nested dictionary"""
+ complex_dict = {"nested": {"key": "value"}, "list": [1, 2, {"inner": "dict"}], "null": None, "bool": True}
+ response = validate_function_response(complex_dict, return_char_limit=1000)
+ # Should be valid JSON
+ import json
+
+ parsed = json.loads(response)
+ assert parsed == complex_dict
+
+
+def test_validate_function_response_dict_truncation():
+ """Test that serialized dict gets truncated properly"""
+ # Create a dict that when serialized will exceed limit
+ large_dict = {"data": "x" * 100}
+ response = validate_function_response(large_dict, return_char_limit=20, truncate=True)
+ assert "NOTE: function output was truncated" in response
+ assert len(response) > 20 # Includes truncation message
+
+
+def test_validate_function_response_empty_string():
+ """Test empty string handling"""
+ response = validate_function_response("", return_char_limit=100)
+ assert response == ""
+
+
+def test_validate_function_response_whitespace():
+ """Test whitespace-only string handling"""
+ response = validate_function_response(" \n\t ", return_char_limit=100)
+ assert response == " \n\t "