Agentic Browser

Documentation

Backend server design

Introduction#

This page describes the backend server design built with FastAPI and a complementary MCP server. It explains the application structure, routing organization, service layer architecture, and the LLM provider abstraction supporting multiple AI models. It documents configuration management across environments, the modular router system for API endpoints, request/response handling patterns, middleware implementation, and error handling strategies. It also covers the separation between API endpoints and business logic, dependency injection patterns, concurrency handling, external service integrations, caching strategies, performance optimization, scalability, load balancing, and monitoring approaches.

Project structure#

The backend consists of:

  • Application entrypoint that selects between API and MCP modes
  • FastAPI application with modular routers and a central run script
  • Core configuration and LLM abstraction
  • Routers for each domain endpoint
  • Services implementing business logic
  • Agents orchestrating multi-step reasoning with tools
  • MCP server exposing tools for external clients

Core components#

  • Entry point and mode selection: supports running as API server or MCP server with optional interactive or non-interactive mode.
  • FastAPI application: defines routes under multiple prefixes and includes health, GitHub, website, YouTube, Google Search, Gmail, Calendar, PyJIIT, React agent, website validator, agent, and file upload endpoints.
  • Configuration: environment-driven settings for host, port, debug level, and Google API key; centralized logger factory.
  • LLM abstraction: provider-agnostic initialization and generation interface supporting Google, OpenAI, Anthropic, Ollama, DeepSeek, and OpenRouter.
  • Routers: per-domain endpoints with dependency injection of services and standardized error handling.
  • Services: business logic implementations for website QA, React agent orchestration, and related tasks.
  • Agents: LangGraph-based reasoning with tool execution and message normalization.
  • MCP server: exposes tools for LLM generation, GitHub Q&A, website markdown fetching, and HTML-to-markdown conversion.

Architecture overview#

The system separates concerns across layers:

  • Presentation: FastAPI routers define endpoints and handle request validation and error mapping.
  • Business logic: Services encapsulate domain-specific workflows.
  • Orchestration: Agents coordinate tool use and multi-step reasoning.
  • Infrastructure: Configuration and LLM abstraction provide pluggable providers and runtime settings.

Detailed component analysis#

FastAPI application and routing organization#

  • Central app definition with title and version.
  • Modular router inclusion under distinct prefixes for health, GitHub, website, YouTube, Google Search, Gmail, Calendar, PyJIIT, React agent, website validator, agent, and file upload.
  • Root endpoint returns app metadata.

Request/Response handling patterns and error handling#

  • Routers validate inputs and delegate to services using dependency injection.
  • Standardized try/catch blocks log errors and raise HTTP exceptions with appropriate status codes.
  • Responses are typed via Pydantic models where applicable.

Dependency injection and service layer#

  • Routers define dependency factories returning service instances.
  • Services encapsulate domain logic and integrate with tools and LLM providers.
  • Example: Website router depends on WebsiteService; React agent router depends on ReactAgentService.

LLM provider abstraction#

  • Provider configurations map provider names to LangChain classes and parameter mappings.
  • Initialization validates provider support, model defaults, API keys, and base URLs.
  • Generation method constructs system and human messages and invokes the underlying client.
  • Default LLM instance is created for application-wide use.

MCP server integration#

  • Defines tools for LLM generation, GitHub Q&A, website markdown fetching, and HTML-to-markdown conversion.
  • Implements tool dispatch based on tool name and arguments.
  • Runs over stdio using MCP server framework.

React agent orchestration#

  • Converts chat history and optional client HTML into LangGraph messages.
  • Builds a graph with an agent node and a tool execution node, conditionally routing between them.
  • Uses cached compilation for performance.

Configuration management#

  • Environment variables drive host, port, debug level, and Google API key.
  • Logging level is derived from debug flag.
  • Centralized logger factory ensures consistent logging across modules.

Middleware implementation#

  • No explicit middleware is defined in the analyzed files. Logging is handled via module loggers and exception handlers in routers.

Concurrency and request handling#

  • FastAPI uses async route handlers; services implement async methods for I/O-bound operations (external APIs, LLM calls).
  • LangGraph invocation is awaited, ensuring cooperative concurrency.

External service integrations#

  • Website service integrates markdown fetching and HTML-to-Markdown conversion.
  • React agent service optionally uploads files to Google GenAI and uses LangChain messages.
  • MCP server integrates with LangChain providers and website tools.

Caching strategies#

  • LangGraph graph is compiled once and cached via a cached graph factory, reducing startup overhead for repeated invocations.

Dependency analysis#

The system exhibits clear layering:

  • Presentation depends on business logic
  • Business logic depends on agents and LLM abstraction
  • Configuration is consumed across layers
  • MCP server reuses LLM and tools

Performance considerations#

  • Async-first design: route handlers and services use async to handle concurrent requests efficiently.
  • Cached graph compilation: reduces repeated graph building costs in the React agent.
  • Minimal synchronous work in hot paths; offloads heavy operations to external services.
  • Environment-driven tuning: adjust debug level and provider/model settings via environment variables.

[No sources needed since this section provides general guidance]

Troubleshooting guide#

  • Missing environment variables: ensure required keys (e.g., provider API keys and base URLs) are set; the LLM initializer raises explicit errors when missing.
  • Router-level validation: routers check required fields and return 400 for invalid requests.
  • Service-level errors: services catch exceptions and return user-friendly messages; routers map unexpected errors to 500.
  • Logging: configure logging level via environment; use module loggers to trace execution paths.

Conclusion#

The backend employs a layered architecture with clear separation between presentation, business logic, orchestration, and infrastructure. FastAPI’s modular routers expose domain-specific endpoints under prefixed namespaces, while dependency injection keeps endpoints thin. The LLM abstraction enables multi-provider support and environment-driven configuration. Asynchronous services and cached graph compilation optimize concurrency and performance. The MCP server extends functionality externally, and reliable error handling ensures predictable responses.

Appendices#

Endpoint catalog#

  • Health: GET /api/genai/health
  • GitHub: GET /api/genai/github
  • Website: POST /api/genai/website
  • YouTube: GET /api/genai/youtube
  • Google Search: GET /api/google-search
  • Gmail: GET /api/gmail
  • Calendar: GET /api/calendar
  • PyJIIT: GET /api/pyjiit
  • React Agent: POST /api/genai/react
  • Website Validator: GET /api/validator
  • Agent: POST /api/agent
  • File Upload: POST /api/upload

Startup and runtime#

  • Entry point accepts mode flags and runs either API or MCP server.
  • API server uses Uvicorn with host/port from configuration.

Scripts and entrypoints#

  • Project scripts expose CLI commands for running API and MCP servers.