mirror of
https://github.com/hyperdxio/hyperdx
synced 2026-04-21 13:37:15 +00:00
## Summary Adds an MCP (Model Context Protocol) server to the HyperDX API, enabling AI assistants (Claude, Cursor, OpenCode, etc.) to query observability data, manage dashboards, and explore data sources directly via standardized tool calls. Key changes: - **MCP server** (`packages/api/src/mcp/`) — Streamable HTTP transport at `/api/mcp`, authenticated via Personal API Access Key - **Tools** — `hyperdx_list_sources`, `hyperdx_query`, `hyperdx_get_dashboard`, `hyperdx_save_dashboard`, `hyperdx_delete_dashboard`, `hyperdx_query_tile` - **Dashboard prompts** — Detailed prompt templates that guide LLMs in generating valid, high-quality dashboards - **Shared logic** — Refactored dashboard validation/transformation out of the external API router into reusable utils (`packages/api/src/routers/external-api/v2/utils/dashboards.ts`) - **Documentation** — `MCP.md` with setup instructions for Claude Code, OpenCode, Cursor, MCP Inspector, and other clients - **Tests** — Unit tests for dashboard tools, query tools, tracing, and response trimming ### Screenshots https://github.com/user-attachments/assets/8c5aa582-c79e-47e0-8f75-e03feabdf8a6 ### How to test locally 1. Start the dev stack: `yarn dev` 2. Connect an MCP client (e.g. MCP Inspector): ```bash cd packages/api && yarn dev:mcp ``` Then configure the inspector: - **Transport Type:** Streamable HTTP - **URL:** `http://localhost:8080/api/mcp` - **Header:** `Authorization: Bearer <your-personal-access-key>` - Click **Connect** 3. Alternatively, connect via Claude Code or OpenCode: ```bash claude mcp add --transport http hyperdx http://localhost:8080/api/mcp \ --header "Authorization: Bearer <your-personal-access-key>" ``` 4. Try listing sources, querying data, or creating/updating a dashboard through the connected AI assistant. 5. Run unit tests: ```bash cd packages/api && yarn ci:unit ``` ### References - Linear Issue: HDX-3710
117 lines
4.2 KiB
TypeScript
117 lines
4.2 KiB
TypeScript
import { ObjectId } from 'mongodb';
|
|
|
|
import type { ExternalDashboardTileWithId } from '@/utils/zod';
|
|
import { externalDashboardTileSchemaWithId } from '@/utils/zod';
|
|
|
|
import { withToolTracing } from '../../utils/tracing';
|
|
import type { ToolDefinition } from '../types';
|
|
import { parseTimeRange, runConfigTile } from './helpers';
|
|
import { hyperdxQuerySchema } from './schemas';
|
|
|
|
// ─── Tool definition ─────────────────────────────────────────────────────────
|
|
|
|
const queryTools: ToolDefinition = (server, context) => {
|
|
const { teamId } = context;
|
|
|
|
server.registerTool(
|
|
'hyperdx_query',
|
|
{
|
|
title: 'Query Data',
|
|
description:
|
|
'Query observability data (logs, metrics, traces) from HyperDX. ' +
|
|
'Use hyperdx_list_sources first to find sourceId/connectionId values. ' +
|
|
'Set displayType to control the query shape.\n\n' +
|
|
'PREFERRED: Use the builder display types (line, stacked_bar, table, number, pie) ' +
|
|
'for aggregated metrics, or "search" for browsing individual log/event rows. ' +
|
|
'These are safer, easier to construct, and cover most use cases.\n\n' +
|
|
'ADVANCED: Use displayType "sql" only when you need capabilities the builder cannot express, ' +
|
|
'such as JOINs, sub-queries, CTEs, or querying tables not registered as sources. ' +
|
|
'Raw SQL requires a connectionId (not sourceId) and a hand-written ClickHouse SQL query.\n\n' +
|
|
'Column naming: Top-level columns are PascalCase (Duration, StatusCode, SpanName). ' +
|
|
"Map attributes use bracket syntax: SpanAttributes['http.method'], ResourceAttributes['service.name']. " +
|
|
'Call hyperdx_list_sources to discover available columns and attribute keys for each source.',
|
|
inputSchema: hyperdxQuerySchema,
|
|
},
|
|
withToolTracing('hyperdx_query', context, async input => {
|
|
const timeRange = parseTimeRange(input.startTime, input.endTime);
|
|
if ('error' in timeRange) {
|
|
return {
|
|
isError: true,
|
|
content: [{ type: 'text' as const, text: timeRange.error }],
|
|
};
|
|
}
|
|
const { startDate, endDate } = timeRange;
|
|
|
|
let tile: ExternalDashboardTileWithId;
|
|
|
|
if (input.displayType === 'sql') {
|
|
tile = externalDashboardTileSchemaWithId.parse({
|
|
id: new ObjectId().toString(),
|
|
name: 'MCP SQL',
|
|
x: 0,
|
|
y: 0,
|
|
w: 24,
|
|
h: 6,
|
|
config: {
|
|
configType: 'sql' as const,
|
|
displayType: 'table' as const,
|
|
connectionId: input.connectionId,
|
|
sqlTemplate: input.sql,
|
|
},
|
|
});
|
|
} else if (input.displayType === 'search') {
|
|
tile = externalDashboardTileSchemaWithId.parse({
|
|
id: new ObjectId().toString(),
|
|
name: 'MCP Search',
|
|
x: 0,
|
|
y: 0,
|
|
w: 24,
|
|
h: 6,
|
|
config: {
|
|
displayType: 'search' as const,
|
|
sourceId: input.sourceId,
|
|
select: input.columns ?? '',
|
|
where: input.where ?? '',
|
|
whereLanguage: input.whereLanguage ?? 'lucene',
|
|
},
|
|
});
|
|
} else {
|
|
tile = externalDashboardTileSchemaWithId.parse({
|
|
id: new ObjectId().toString(),
|
|
name: 'MCP Query',
|
|
x: 0,
|
|
y: 0,
|
|
w: 12,
|
|
h: 4,
|
|
config: {
|
|
displayType: input.displayType,
|
|
sourceId: input.sourceId,
|
|
select: input.select.map(s => ({
|
|
aggFn: s.aggFn,
|
|
where: s.where ?? '',
|
|
whereLanguage: s.whereLanguage ?? 'lucene',
|
|
valueExpression: s.valueExpression,
|
|
alias: s.alias,
|
|
level: s.level,
|
|
})),
|
|
groupBy: input.groupBy ?? undefined,
|
|
orderBy: input.orderBy ?? undefined,
|
|
...(input.granularity ? { granularity: input.granularity } : {}),
|
|
},
|
|
});
|
|
}
|
|
|
|
return runConfigTile(
|
|
teamId.toString(),
|
|
tile,
|
|
startDate,
|
|
endDate,
|
|
input.displayType === 'search'
|
|
? { maxResults: input.maxResults }
|
|
: undefined,
|
|
);
|
|
}),
|
|
);
|
|
};
|
|
|
|
export default queryTools;
|