-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(mcp): add get_span_details tool #3255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f9c3573
feat(mcp): add get_span_details tool
ericallam c2fdff7
Some fixes and changesets
ericallam 922c34f
Fix for coderabbit suggestion
ericallam 9a5e7c0
fixed the duration formatter to work with nanoseconds
ericallam 6d9e386
just treat duration as nanoseconds, don't worry about the postgresql …
ericallam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@trigger.dev/core": patch | ||
| "trigger.dev": patch | ||
| --- | ||
|
|
||
| Add `get_span_details` MCP tool for inspecting individual spans within a run trace. | ||
|
|
||
| - New `get_span_details` tool returns full span attributes, timing, events, and AI enrichment (model, tokens, cost, speed) | ||
| - Span IDs now shown in `get_run_details` trace output for easy discovery | ||
| - New API endpoint `GET /api/v1/runs/:runId/spans/:spanId` | ||
| - New `retrieveSpan()` method on the API client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: feature | ||
| --- | ||
|
|
||
| Add API endpoint `GET /api/v1/runs/:runId/spans/:spanId` that returns detailed span information including properties, events, AI enrichment (model, tokens, cost), and triggered child runs. |
134 changes: 134 additions & 0 deletions
134
apps/webapp/app/routes/api.v1.runs.$runId.spans.$spanId.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { BatchId } from "@trigger.dev/core/v3/isomorphic"; | ||
| import { z } from "zod"; | ||
| import { $replica } from "~/db.server"; | ||
| import { extractAISpanData } from "~/components/runs/v3/ai"; | ||
| import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { resolveEventRepositoryForStore } from "~/v3/eventRepository/index.server"; | ||
| import { getTaskEventStoreTableForRun } from "~/v3/taskEventStore.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| runId: z.string(), | ||
| spanId: z.string(), | ||
| }); | ||
|
|
||
| export const loader = createLoaderApiRoute( | ||
| { | ||
| params: ParamsSchema, | ||
| allowJWT: true, | ||
| corsStrategy: "all", | ||
| findResource: (params, auth) => { | ||
| return $replica.taskRun.findFirst({ | ||
| where: { | ||
| friendlyId: params.runId, | ||
| runtimeEnvironmentId: auth.environment.id, | ||
| }, | ||
| }); | ||
| }, | ||
| shouldRetryNotFound: true, | ||
| authorization: { | ||
| action: "read", | ||
| resource: (run) => ({ | ||
| runs: run.friendlyId, | ||
| tags: run.runTags, | ||
| batch: run.batchId ? BatchId.toFriendlyId(run.batchId) : undefined, | ||
| tasks: run.taskIdentifier, | ||
| }), | ||
| superScopes: ["read:runs", "read:all", "admin"], | ||
| }, | ||
| }, | ||
| async ({ params, resource: run, authentication }) => { | ||
| const eventRepository = resolveEventRepositoryForStore(run.taskEventStore); | ||
| const eventStore = getTaskEventStoreTableForRun(run); | ||
|
|
||
| const span = await eventRepository.getSpan( | ||
| eventStore, | ||
| authentication.environment.id, | ||
| params.spanId, | ||
| run.traceId, | ||
| run.createdAt, | ||
| run.completedAt ?? undefined | ||
| ); | ||
|
|
||
| if (!span) { | ||
| return json({ error: "Span not found" }, { status: 404 }); | ||
| } | ||
|
|
||
| // Duration is nanoseconds from ClickHouse (Postgres store is deprecated) | ||
| const durationMs = span.duration / 1_000_000; | ||
|
|
||
| const aiData = | ||
| span.properties && typeof span.properties === "object" | ||
| ? extractAISpanData(span.properties as Record<string, unknown>, durationMs) | ||
| : undefined; | ||
|
|
||
| const triggeredRuns = await $replica.taskRun.findMany({ | ||
| take: 50, | ||
| select: { | ||
| friendlyId: true, | ||
| taskIdentifier: true, | ||
| status: true, | ||
| createdAt: true, | ||
| }, | ||
| where: { | ||
| runtimeEnvironmentId: authentication.environment.id, | ||
| parentSpanId: params.spanId, | ||
| }, | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const properties = | ||
| span.properties && | ||
| typeof span.properties === "object" && | ||
| Object.keys(span.properties as Record<string, unknown>).length > 0 | ||
| ? (span.properties as Record<string, unknown>) | ||
| : undefined; | ||
|
|
||
| return json( | ||
| { | ||
| spanId: span.spanId, | ||
| parentId: span.parentId, | ||
| runId: run.friendlyId, | ||
| message: span.message, | ||
| isError: span.isError, | ||
| isPartial: span.isPartial, | ||
| isCancelled: span.isCancelled, | ||
| level: span.level, | ||
| startTime: span.startTime, | ||
| durationMs, | ||
| properties, | ||
| events: span.events?.length ? span.events : undefined, | ||
| entityType: span.entity.type ?? undefined, | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ai: aiData | ||
| ? { | ||
| model: aiData.model, | ||
| provider: aiData.provider, | ||
| operationName: aiData.operationName, | ||
| inputTokens: aiData.inputTokens, | ||
| outputTokens: aiData.outputTokens, | ||
| totalTokens: aiData.totalTokens, | ||
| cachedTokens: aiData.cachedTokens, | ||
| reasoningTokens: aiData.reasoningTokens, | ||
| inputCost: aiData.inputCost, | ||
| outputCost: aiData.outputCost, | ||
| totalCost: aiData.totalCost, | ||
| tokensPerSecond: aiData.tokensPerSecond, | ||
| msToFirstChunk: aiData.msToFirstChunk, | ||
| durationMs: aiData.durationMs, | ||
| finishReason: aiData.finishReason, | ||
| responseText: aiData.responseText, | ||
| } | ||
| : undefined, | ||
| triggeredRuns: | ||
| triggeredRuns.length > 0 | ||
| ? triggeredRuns.map((r) => ({ | ||
| runId: r.friendlyId, | ||
| taskIdentifier: r.taskIdentifier, | ||
| status: r.status, | ||
| createdAt: r.createdAt, | ||
| })) | ||
| : undefined, | ||
| }, | ||
| { status: 200 } | ||
| ); | ||
| } | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.