Query Events API Reference
The Query Events API enables logging and querying tool retrieval events for dashboard visibility and observability.Overview
Purpose
- Log tool retrieval queries with metadata (selected tools, scores, source)
- Retrieve historical queries for analytics and debugging
- Track request origin (CLI, API, Dashboard)
- Monitor tool selection patterns and performance
Base URL
Endpoints
/v2/retrieve_tools — Tool Retrieval (with Logging)
Automatically logs to query_events when user is authenticated.
Endpoint: GET /v2/retrieve_tools
Description: Retrieve tools matching a query. Logs the request and results to query_events table automatically.
Request
| Parameter | Type | Required | Description |
|---|---|---|---|
query | string | Yes | Natural language query (e.g., “deploy to production”) |
top_k | integer | No | Number of tools to return (default: 5, max: 50) |
filter.server | string | No | Filter by MCP server name |
| Header | Value | Required | Description |
|---|---|---|---|
Authorization | Bearer {api_key} | Yes | Your API key (for logging and auth) |
X-Agent-Source | mcp | api | dashboard | No | Request origin (default: api) |
Response
Status: 200 OK| Field | Type | Description |
|---|---|---|
tools | array | Retrieved tools with metadata |
tools[].name | string | Tool name (unique within server) |
tools[].server | string | MCP server name |
tools[].score | number | Overall relevance score (0-1) |
tools[].semantic_score | number | Semantic relevance score |
tools[].capability_score | number | Capability match score |
tools[].success_rate | number | Historical success rate |
tools[].description | string | Tool description |
tools[].input_schema | object | JSON Schema for inputs |
query_id | string | Unique query ID (UUID) |
Side Effects
When authenticated, automatically logs toquery_events table:
Examples
CLI Request (with MCP header):source: "mcp".
API Request (no special header):
source: "api" (default).
Dashboard Request:
source: "dashboard".
QueryEventService (Backend)
TheQueryEventService is the modular service responsible for logging queries.
Import
Methods
__init__(supabase_url: Optional[str] = None, service_key: Optional[str] = None)
Initialize the service with Supabase credentials.
Parameters:
SUPABASE_URLSUPABASE_SERVICE_KEY
QueryEventService instance
async log_retrieval(user_id, query, source, tools, scores, timeout)
Log a tool retrieval to the query_events table.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
user_id | str | Yes | User ID (UUID). Required for logging. |
query | str | Yes | Natural language query |
source | str | No | Request source: "api", "mcp", "dashboard" (default: "api") |
tools | list[dict] | No | Full list of retrieved tools with metadata |
scores | dict | No | Tool name → scores mapping |
timeout | float | No | HTTP timeout in seconds (default: 2.0) |
str | None — Inserted event ID (UUID) or None if failed
Side Effects:
- Writes to Supabase
query_eventstable - Logs warnings if HTTP/network fails (non-blocking)
- Returns gracefully if Supabase unavailable
is_ready() -> bool
Check if service is properly configured (has Supabase credentials).
Returns: True if ready, False otherwise
Example:
Query Events Table Schema
Querying Query Events
Via Supabase REST API
Get user’s recent queries:Via Python
Errors
401 Unauthorized
Cause: Missing or invalid API key Response:Authorization: Bearer {api_key} header
400 Bad Request
Cause: Missing required parameters Response:500 Internal Server Error
Cause: Backend error (e.g., Supabase unavailable) Response:Rate Limiting
| Tier | Queries/min | Queries/day |
|---|---|---|
| Free | 60 | 10,000 |
| Pro | 600 | 100,000 |
| Enterprise | Unlimited | Unlimited |
Authentication
All endpoints require authentication:Best Practices
1. Always Include Source Header
Help analytics by identifying request origin:2. Handle Gracefully
Query event logging is non-blocking and fire-and-forget:3. Monitor Query Volume
Track queries per minute and patterns:4. Archive Old Events
Keep dashboard responsive by archiving:Related Docs
Key Takeaway: Query events provide complete visibility into tool retrieval patterns. They’re logged automatically and never block requests. Use them for analytics, debugging, and monitoring. 📊