Agentic Browser

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

API overview

Introduction#

FastAPI layout: routers under service prefixes, startup, logging, and how auth tokens ride on request bodies. CORS middleware allows all origins.

Project structure#

The API server is organized around a FastAPI application in main.py that aggregates multiple routers under distinct base prefixes. Each router corresponds to a functional area (e.g., GenAI, Google Search, Gmail, Calendar, PyJiit, React Agent, Website, Validator, Upload). MCP is mounted at /mcp on the same process. Stdio MCP is the agentic-mcp script.

Core components#

  • Application factory and router mounts: The FastAPI app is created and routers are included under base prefixes to segment functionality by domain.
  • Startup orchestration: main.py starts FastAPI and mounts MCP at /mcp. Stdio MCP is agentic-mcp.
  • Configuration and logging: Environment variables drive runtime behavior and logging level; a shared logger getter is used across modules.
  • Health endpoint: A dedicated router exposes a simple health check returning a structured response model.

Key implementation references:

  • Application creation and router mounts: main.py
  • API runner: main.py (run() / agentic-api-run)
  • Configuration and logging: core/config.py, core/config.py
  • Health response model: models/response/health.py
  • Health handler: routers/health.py

Architecture overview#

The API follows a modular FastAPI architecture:

  • Central app definition and router composition
  • Feature-based routers grouped under base prefixes
  • Request validation via Pydantic models
  • Service-layer delegation per router
  • Global exception handling returning standardized HTTP errors
  • Optional root endpoint returning app metadata

Detailed component analysis#

Endpoint organization and base URL patterns#

Routers are mounted under the following base prefixes to separate service domains:

  • GenAI family: /api/genai/health, /api/genai/github, /api/genai/website, /api/genai/youtube, /api/genai/react
  • Google family: /api/google-search
  • Productivity: /api/gmail, /api/calendar, /api/pyjiit
  • Validation and utilities: /api/validator, /api/agent, /api/upload

These mounts are defined in the central app file and exported via the routers package.

Health check endpoint#

  • Path: GET /api/genai/health
  • Handler: Returns a structured response indicating service health
  • Response model: HealthResponse with status and message fields

Website answer endpoint#

  • Path: POST /api/genai/website
  • Request model: WebsiteRequest (validated in router)
  • Service: WebsiteService injected via dependency
  • Behavior: Validates presence of URL and question; delegates to service; returns answer in a dictionary
  • Error handling: Raises HTTP 400 for missing fields; wraps unexpected errors as HTTP 500

Google search endpoint#

  • Path: POST /api/google-search
  • Request model: SearchRequest with query and optional max_results
  • Service: GoogleSearchService injected via dependency
  • Behavior: Validates query; calls service.search; returns results in a dictionary
  • Error handling: Raises HTTP 400 for invalid input; wraps unexpected errors as HTTP 500

Gmail endpoints#

  • Paths:
  • POST /api/gmail/unread
  • POST /api/gmail/latest
  • POST /api/gmail/mark_read
  • POST /api/gmail/send
  • Request models: UnreadRequest, LatestRequest, MarkReadRequest, SendEmailRequest (all require access_token)
  • Service: GmailService injected via dependency
  • Behavior: Validates required fields; calls appropriate service methods; returns structured results
  • Error handling: Raises HTTP 400 for missing fields; wraps unexpected errors as HTTP 500

Calendar endpoints#

  • Paths:
  • POST /api/calendar/events
  • POST /api/calendar/create
  • Request models: EventsRequest (requires access_token), CreateEventRequest (requires access_token, summary, start_time, end_time)
  • Service: CalendarService injected via dependency
  • Behavior: Validates required fields and ISO 8601 timestamps; calls service methods; returns structured results
  • Error handling: Raises HTTP 400 for invalid input; wraps unexpected errors as HTTP 500

React agent endpoint#

  • Path: POST /api/genai/react
  • Request model: CrawlerRequest (validated in router)
  • Service: ReactAgentService injected via dependency
  • Behavior: Validates question; delegates to service.generate_answer; returns CrawllerResponse
  • Error handling: Raises HTTP 400 for missing question; wraps unexpected errors as HTTP 500

Application initialization, middleware, and global settings#

  • Application creation: FastAPI app is instantiated with title and version.
  • Router mounts: Routers are imported from the routers package and mounted under base prefixes.
  • Root endpoint: Optional GET “/” returns app metadata.
  • CORS middleware is configured in main.py with allow_origins=["*"].
  • Logging: Centralized logger getter is used across modules; environment variables control debug level and backend host/port.

Authentication mechanisms#

  • Access tokens are passed via request bodies for sensitive operations:
  • Gmail: access_token required for all endpoints
  • Calendar: access_token required for all endpoints
  • Website: no token required in the endpoint shown
  • Google Search: no token required in the endpoint shown
  • The code does not implement bearer token middleware or route guards; token validation occurs inside each endpoint’s request model and service invocation.

CORS configuration#

  • main.py adds CORSMiddleware with allow_origins=["*"], allow_credentials=False, and all methods/headers.

Error handling strategies#

  • Centralized try/catch blocks in each endpoint:
  • Raise HTTP 400 for invalid input (missing fields, malformed data)
  • Wrap unexpected exceptions as HTTP 500 with sanitized details
  • Services log exceptions internally; endpoints re-raise as HTTP exceptions to maintain consistent error responses.

Monitoring interfaces#

  • Health endpoint: GET /api/genai/health provides a simple readiness/liveness indicator.
  • Logging: Centralized logger is used across modules; environment controls verbosity.

Basic API usage patterns and integration scenarios#

  • Start the API server:
  • python main.py or agentic-api-run.
  • The API listens on host and port configured via environment variables.
  • Example flows:
  • Website QA: POST to /api/genai/website with a URL and question; receive an answer.
  • Google Search: POST to /api/google-search with a query and optional max_results; receive results.
  • Gmail operations: Use access_token in request bodies to list unread messages, fetch latest messages, mark read, or send emails.
  • Calendar operations: Use access_token to list events or create events with ISO 8601 start/end times.
  • Health check: GET /api/genai/health to verify service availability.

Dependency analysis#

The API module composes routers and services . Routers depend on service classes, which encapsulate tool integrations. The central app depends on the routers package for imports and mounts.

Performance considerations#

  • Keep request payloads minimal; avoid unnecessary fields to reduce parsing overhead.
  • Validate inputs early in routers to fail fast and reduce downstream processing.
  • Reuse injected services per request to minimize initialization costs.
  • Monitor logs and adjust logging level via environment variables for production deployments.

Troubleshooting guide#

  • Health check failures:
  • Verify GET /api/genai/health returns the expected status and message.
  • 400 Bad Request errors:
  • Ensure required fields are present (e.g., query, access_token, question).
  • 500 Internal Server Errors:
  • Inspect logs for exception traces; services already log exceptions internally.
  • Startup issues:
  • Confirm environment variables for host/port and debug level are set appropriately.
  • Start the API with python main.py or agentic-api-run.

Conclusion#

Routers own their prefixes. Health is a plain endpoint. Tokens are checked per router where needed. CORS is already on in main.py; tighten origins for production.

Appendices#

  • Startup: python main.py or agentic-api-run. Stdio MCP: agentic-mcp.