Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

Backend server design

Introduction#

FastAPI app structure, routers, services, LLM abstraction, config, errors, and concurrency. How the MCP server sits beside the REST API.

Project structure#

The backend consists of:

  • FastAPI application in main.py with modular routers
  • MCP server in mcp_server/ (stdio via agentic-mcp, HTTP at /mcp)
  • Core configuration and LLM abstraction
  • Routers for each domain endpoint
  • Services implementing business logic
  • Agents orchestrating multi-step reasoning with tools
  • Memory stores (sqlmodel/asyncpg, Neo4j, OpenSearch)

Core components#

  • Entry point: main.py starts FastAPI with Uvicorn and mounts MCP at /mcp. Stdio MCP is agentic-mcp.
  • 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 so the event loop stays cooperative.

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#

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.

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 plain messages; routers map unexpected errors to 500.
  • Logging: configure logging level via environment; use module loggers to trace execution paths.

Conclusion#

Thin routers, fat services, awaited LangGraph calls. Start the API with python main.py or agentic-api-run. Stdio MCP is agentic-mcp. Config comes from the environment.

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 main.py runs FastAPI via Uvicorn and mounts MCP at /mcp.
  • Stdio MCP is agentic-mcp.
  • API host/port come from configuration.

Scripts and entrypoints#

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