Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: preserve LLM and MCP settings in migration 108 (#14112)

Graham Neubig29577935b48b
diff --git a/enterprise/migrations/versions/108_add_agent_settings_to_enterprise_settings.py b/enterprise/migrations/versions/108_add_agent_settings_to_enterprise_settings.py
index 209818da76..2c5859e98a 100644
--- a/enterprise/migrations/versions/108_add_agent_settings_to_enterprise_settings.py
+++ b/enterprise/migrations/versions/108_add_agent_settings_to_enterprise_settings.py
@@ -62,6 +62,7 @@ def _build_user_agent_settings(row: Mapping[str, Any]) -> dict[str, Any]:
'enabled': row['enable_default_condenser'],
'max_size': row['condenser_max_size'],
},
+ 'mcp_config': row['mcp_config'],
}
)
return _deep_merge(generated, row.get('agent_settings') or {})
@@ -276,6 +277,7 @@ def upgrade() -> None:
sa.column('llm_base_url', sa.String()),
sa.column('enable_default_condenser', sa.Boolean()),
sa.column('condenser_max_size', sa.Integer()),
+ sa.column('mcp_config', sa.JSON()),
sa.column('agent_settings', sa.JSON()),
sa.column('conversation_settings', sa.JSON()),
)
@@ -290,6 +292,7 @@ def upgrade() -> None:
user_settings_table.c.llm_base_url,
user_settings_table.c.enable_default_condenser,
user_settings_table.c.condenser_max_size,
+ user_settings_table.c.mcp_config,
user_settings_table.c.agent_settings,
user_settings_table.c.conversation_settings,
)
diff --git a/enterprise/storage/user_store.py b/enterprise/storage/user_store.py
index 575baaf33c..ac52d784e3 100644
--- a/enterprise/storage/user_store.py
+++ b/enterprise/storage/user_store.py
@@ -230,19 +230,10 @@ class UserStore:
from storage.org_store import OrgStore
- org_kwargs = OrgStore.get_kwargs_from_user_settings(decrypted_user_settings)
- org_kwargs.pop('id', None)
-
- # If the user has custom settings, keep the org defaults minimal.
- if custom_settings:
- org_kwargs['agent_settings'] = {
- 'schema_version': AGENT_SETTINGS_SCHEMA_VERSION,
- 'llm': {
- 'model': get_default_litellm_model(),
- 'base_url': LITE_LLM_API_URL,
- },
- }
- org_kwargs['org_version'] = ORG_SETTINGS_VERSION
+ org_kwargs = UserStore._get_org_kwargs_for_migration(
+ decrypted_user_settings,
+ custom_settings=custom_settings,
+ )
for key, value in org_kwargs.items():
if hasattr(org, key):
@@ -1066,6 +1057,27 @@ class UserStore:
already_migrated=False,
)
+ @staticmethod
+ def _get_org_kwargs_for_migration(
+ user_settings: UserSettings, *, custom_settings: bool
+ ) -> dict:
+ from storage.org_store import OrgStore
+
+ org_kwargs = OrgStore.get_kwargs_from_user_settings(user_settings)
+ org_kwargs.pop('id', None)
+ org_kwargs['org_version'] = ORG_SETTINGS_VERSION
+
+ if custom_settings:
+ org_kwargs['agent_settings'] = {
+ 'schema_version': AGENT_SETTINGS_SCHEMA_VERSION,
+ 'llm': {
+ 'model': get_default_litellm_model(),
+ 'base_url': LITE_LLM_API_URL,
+ },
+ }
+
+ return org_kwargs
+
@staticmethod
def _has_custom_settings(
user_settings: UserSettings, old_user_version: int | None
diff --git a/enterprise/tests/unit/test_migration_108_add_agent_settings.py b/enterprise/tests/unit/test_migration_108_add_agent_settings.py
index 21efc8c2df..030b40973a 100644
--- a/enterprise/tests/unit/test_migration_108_add_agent_settings.py
+++ b/enterprise/tests/unit/test_migration_108_add_agent_settings.py
@@ -25,6 +25,7 @@ def test_user_settings_are_split_into_agent_and_conversation_buckets():
'llm_base_url': 'https://api.example.com',
'enable_default_condenser': False,
'condenser_max_size': 128,
+ 'mcp_config': {'mcpServers': {'admin': {'url': 'https://mcp.example.com'}}},
'agent_settings': {},
'conversation_settings': {},
}
@@ -40,6 +41,7 @@ def test_user_settings_are_split_into_agent_and_conversation_buckets():
'base_url': 'https://api.example.com',
},
'condenser': {'enabled': False, 'max_size': 128},
+ 'mcp_config': {'mcpServers': {'admin': {'url': 'https://mcp.example.com'}}},
}
assert conversation_settings == {
'max_iterations': 42,
@@ -74,6 +76,41 @@ def test_org_member_diffs_use_nested_llm_and_conversation_settings():
assert conversation_settings_diff == {'max_iterations': 50}
+def test_org_settings_are_split_into_agent_and_conversation_buckets():
+ row = {
+ 'agent': 'CodeActAgent',
+ 'default_max_iterations': 99,
+ 'security_analyzer': 'auto',
+ 'confirmation_mode': False,
+ 'default_llm_model': 'anthropic/claude-3-7-sonnet',
+ 'default_llm_base_url': 'https://api.example.com',
+ 'enable_default_condenser': True,
+ 'condenser_max_size': 256,
+ 'mcp_config': {'mcpServers': {'org': {'url': 'https://org-mcp.example.com'}}},
+ 'agent_settings': {},
+ 'conversation_settings': {},
+ }
+
+ agent_settings = migration_108._build_org_agent_settings(row)
+ conversation_settings = migration_108._build_org_conversation_settings(row)
+
+ assert agent_settings == {
+ 'schema_version': 1,
+ 'agent': 'CodeActAgent',
+ 'llm': {
+ 'model': 'anthropic/claude-3-7-sonnet',
+ 'base_url': 'https://api.example.com',
+ },
+ 'condenser': {'enabled': True, 'max_size': 256},
+ 'mcp_config': {'mcpServers': {'org': {'url': 'https://org-mcp.example.com'}}},
+ }
+ assert conversation_settings == {
+ 'max_iterations': 99,
+ 'confirmation_mode': False,
+ 'security_analyzer': 'auto',
+ }
+
+
def test_downgrade_extracts_legacy_values_from_nested_settings():
row = {
'agent_settings': {
@@ -114,6 +151,7 @@ def test_migrated_payload_loads_via_user_settings_to_settings():
'llm_base_url': 'https://api.example.com',
'enable_default_condenser': False,
'condenser_max_size': 128,
+ 'mcp_config': {'mcpServers': {'admin': {'url': 'https://mcp.example.com'}}},
'agent_settings': {},
'conversation_settings': {},
}
@@ -130,6 +168,11 @@ def test_migrated_payload_loads_via_user_settings_to_settings():
assert settings.agent_settings.llm.base_url == 'https://api.example.com'
assert settings.agent_settings.condenser.enabled is False
assert settings.agent_settings.condenser.max_size == 128
+ assert settings.agent_settings.mcp_config is not None
+ assert (
+ settings.agent_settings.mcp_config.mcpServers['admin'].url
+ == 'https://mcp.example.com'
+ )
assert settings.conversation_settings.max_iterations == 42
assert settings.conversation_settings.confirmation_mode is True
assert settings.conversation_settings.security_analyzer == 'llm'
diff --git a/enterprise/tests/unit/test_user_store.py b/enterprise/tests/unit/test_user_store.py
index 52db00e9f1..bd03b7640a 100644
--- a/enterprise/tests/unit/test_user_store.py
+++ b/enterprise/tests/unit/test_user_store.py
@@ -695,6 +695,66 @@ async def test_list_users(async_session_maker):
assert user_id2 in user_ids
+def test_get_org_kwargs_for_migration_preserves_existing_llm_when_not_custom():
+ from server.constants import ORG_SETTINGS_VERSION
+ from storage.user_settings import UserSettings
+
+ user_settings = UserSettings(
+ keycloak_user_id='test',
+ user_version=3,
+ agent_settings={
+ 'schema_version': 1,
+ 'llm': {
+ 'model': 'anthropic/claude-sonnet-4-5-20250929',
+ 'base_url': 'https://api.anthropic.com/v1',
+ },
+ },
+ conversation_settings={'max_iterations': 42},
+ )
+
+ org_kwargs = UserStore._get_org_kwargs_for_migration(
+ user_settings, custom_settings=False
+ )
+
+ assert org_kwargs['org_version'] == ORG_SETTINGS_VERSION
+ assert org_kwargs['agent_settings'] == user_settings.agent_settings
+ assert org_kwargs['conversation_settings'] == user_settings.conversation_settings
+
+
+def test_get_org_kwargs_for_migration_uses_minimal_org_defaults_for_custom_llm():
+ from server.constants import (
+ LITE_LLM_API_URL,
+ ORG_SETTINGS_VERSION,
+ get_default_litellm_model,
+ )
+ from storage.user_settings import UserSettings
+
+ user_settings = UserSettings(
+ keycloak_user_id='test',
+ user_version=3,
+ agent_settings={
+ 'schema_version': 1,
+ 'llm': {
+ 'model': 'anthropic/claude-sonnet-4-5-20250929',
+ 'base_url': 'https://api.anthropic.com/v1',
+ },
+ },
+ )
+
+ org_kwargs = UserStore._get_org_kwargs_for_migration(
+ user_settings, custom_settings=True
+ )
+
+ assert org_kwargs['org_version'] == ORG_SETTINGS_VERSION
+ assert org_kwargs['agent_settings'] == {
+ 'schema_version': 1,
+ 'llm': {
+ 'model': get_default_litellm_model(),
+ 'base_url': LITE_LLM_API_URL,
+ },
+ }
+
+
# --- Tests for _has_custom_settings ---