Skip to content

[PERFORMANCE]: Replace stdlib json with orjson for consistency and performance #2113

@crivetimihai

Description

@crivetimihai

Summary

The codebase extensively uses orjson for JSON serialization/deserialization, but there are still a few places using stdlib json or httpx's response.json() (which uses stdlib json internally). Converting these to orjson would provide:

  • Consistency: Single JSON library throughout the codebase
  • Performance: orjson is 3-10x faster than stdlib json
  • Memory efficiency: orjson has lower memory overhead

Impact

  • CPU savings: Estimated <1-2% improvement
  • Effort: Low (straightforward replacements)
  • Risk: Low (drop-in replacement)

1. Direct stdlib json usage

mcpgateway/services/mcp_session_pool.py:340

# Current (line 34, 340)
import json
serialized_identity = json.dumps(identity_parts)

# Recommended
import orjson
serialized_identity = orjson.dumps(identity_parts, option=orjson.OPT_SORT_KEYS).decode()

Context: Creates deterministic hash for session pool identity keys.


2. httpx response.json() calls (uses stdlib json internally)

Replace response.json() with orjson.loads(response.content) for consistency.

mcpgateway/services/oauth_manager.py (5 instances)

  • Line 273: token_response = response.json()
  • Line 372: token_response = response.json()
  • Line 483: token_response = response.json()
  • Line 1063: token_response = response.json()
  • Line 1128: token_response = response.json()

mcpgateway/services/sso_service.py (3 instances)

  • Line 466: return response.json()
  • Line 488: user_data = response.json()
  • Line 495: orgs_data = orgs_response.json()

mcpgateway/services/tool_service.py (3 instances)

  • Line 2638: result = response.json()
  • Line 2648: result = response.json()
  • Line 3985: return http_response.json()

mcpgateway/services/dcr_service.py (6 instances)

  • Line 94: metadata = response.json()
  • Line 115: metadata = response.json()
  • Line 178: registration_response = response.json()
  • Line 180: error_data = response.json()
  • Line 292: updated_response = response.json()
  • Line 304: error_data = response.json()

mcpgateway/services/gateway_service.py (2 instances)

  • Line 2748: result = response.json()
  • Line 2892: result = response.json()

mcpgateway/services/llm_proxy_service.py (1 instance)

  • Line 434: data = response.json()

mcpgateway/services/a2a_service.py (1 instance)

  • Line 1198: response = http_response.json()

mcpgateway/utils/keycloak_discovery.py (2 instances)

  • Line 54: config = response.json()
  • Line 111: config = response.json()

mcpgateway/routers/llm_admin_router.py (1 instance)

  • Line 718: data = response.json()

mcpgateway/cache/session_registry.py (1 instance)

  • Line 1545: result = rpc_response.json()

mcpgateway/admin.py (1 instance)

  • Line 12342: response_body = response.json()

mcpgateway/cli_export_import.py (1 instance)

  • Line 102: return response.json()

Conversion Pattern

# Before
data = response.json()

# After
import orjson
data = orjson.loads(response.content)

For error handling:

# Before
try:
    data = response.json()
except json.JSONDecodeError:
    ...

# After  
try:
    data = orjson.loads(response.content)
except orjson.JSONDecodeError:
    ...

Summary

Category Count Files
Direct json.dumps 1 mcp_session_pool.py
httpx response.json() 27 12 files
Total 28 13 files

Out of Scope

  • JavaScript code (static/admin.js, templates): Uses browser's native JSON parser
  • Test files: May intentionally use stdlib json for compatibility
  • Comments/docstrings: Not actual code

Checklist

  • Replace json.dumps in mcp_session_pool.py
  • Replace response.json() calls with orjson.loads(response.content) in:
    • oauth_manager.py (5)
    • sso_service.py (3)
    • tool_service.py (3)
    • dcr_service.py (6)
    • gateway_service.py (2)
    • llm_proxy_service.py (1)
    • a2a_service.py (1)
    • keycloak_discovery.py (2)
    • llm_admin_router.py (1)
    • session_registry.py (1)
    • admin.py (1)
    • cli_export_import.py (1)
  • Update exception handling from json.JSONDecodeError to orjson.JSONDecodeError where applicable
  • Remove import json from mcp_session_pool.py

Metadata

Metadata

Assignees

Labels

performancePerformance related itemspythonPython / backend development (FastAPI)

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions