:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useValues } from 'kea'
import { IconBolt, IconClock, IconSparkles, IconUser, IconWarning } from '@posthog/icons'
import { LemonButton, LemonSkeleton, LemonTag } from '@posthog/lemon-ui'
import { Button, Spinner } from '@posthog/quill-primitives'
import { AccessControlAction } from 'lib/components/AccessControlAction'
import { CopyToClipboardInline } from 'lib/components/CopyToClipboard'
import { AccessControlLevel, AccessControlResourceType } from '~/types'
import { MCP_ANALYTICS_SESSION_FEEDBACK_PROMPT } from '../feedback/constants'
import { MCPAnalyticsFeedbackPrompt } from '../feedback/MCPAnalyticsFeedbackPrompt'
import { mcpSessionsLogic } from './mcpSessionsLogic'
import { formatDuration, formatRelativeOffset, sessionDurationMs, shortenSessionId } from './utils'
function MetaBadge({ icon, label }: { icon: React.ReactNode; label: React.ReactNode }): JSX.Element {
return (
<span className="inline-flex items-center gap-1 rounded-full bg-surface-secondary px-2 py-0.5 text-[11px] text-secondary">
<span className="text-[11px] leading-none">{icon}</span>
{label}
</span>
)
}
function IdTooltip({ label, value }: { label: string; value: string }): JSX.Element {
return (
<div className="max-w-xs break-all">
{label}: <span className="font-mono">{value}</span>
<div className="mt-1 opacity-75">Click to copy</div>
</div>
)
}
export function MCPSessionDetail(): JSX.Element {
const { selectedSession, selectedSessionToolCalls, selectedSessionIntent, isSelectedSessionGenerating } =
useValues(mcpSessionsLogic)
const { generateIntent, loadMoreToolCalls, loadToolCalls } = useActions(mcpSessionsLogic)
if (!selectedSession) {
return (
<div className="flex h-full items-center justify-center text-sm text-secondary p-6 text-center">
Pick a session to step through its tool calls.
</div>
)
}
const durationMs = sessionDurationMs(selectedSession.session_start, selectedSession.session_end)
const calls = selectedSession.tool_calls
const identifiedPerson = !!(selectedSession.person_name || selectedSession.person_email)
const primaryLabel = identifiedPerson
? selectedSession.person_name || selectedSession.person_email || selectedSession.distinct_id
: selectedSession.distinct_id || 'unknown'
// The panel's view of the selected session's calls: the list, whether more pages exist, whether
// the first page is still loading (skeleton), and whether a "Load more" append is in flight.
const { calls: toolCalls, hasNext, loading, loadingMore, error } = selectedSessionToolCalls
return (
<div className="flex flex-col h-full min-h-0">
<header className="flex flex-col gap-2 shrink-0 border-b border-primary px-3 pt-3 pb-2">
<div className="flex items-center gap-2 min-w-0">
{loading ? (
<LemonSkeleton.Circle className="h-9 w-9" />
) : (
<div
className={`flex h-9 w-9 shrink-0 items-center justify-center rounded-full ${
identifiedPerson ? 'bg-accent/20 text-accent' : 'bg-surface-secondary text-secondary'
}`}
>
<IconUser className="text-2xl" />
</div>
)}
<div className="flex flex-col min-w-0 flex-1 gap-1">
{loading ? (
<>
<LemonSkeleton className="h-4 w-40" />
<LemonSkeleton className="h-3 w-28" />
</>
) : (
<>
<span
className={`truncate font-semibold ${
identifiedPerson ? 'text-default text-sm' : 'font-mono text-xs text-secondary'
}`}
>
{primaryLabel}
</span>
{selectedSession.distinct_id ? (
<CopyToClipboardInline
explicitValue={selectedSession.distinct_id}
description="distinct id"
iconSize="xsmall"
tooltipMessage={
<IdTooltip label="Distinct ID" value={selectedSession.distinct_id} />
}
className="truncate font-mono text-[11px] text-secondary"
>
{selectedSession.distinct_id}
</CopyToClipboardInline>
) : null}
</>
)}
</div>
</div>
<div className="flex flex-wrap items-center gap-1.5">
{loading ? (
<LemonSkeleton repeat={4} className="h-5 w-20 rounded-full" />
) : (
<>
{selectedSession.mcp_client_name ? (
<MetaBadge icon={<IconSparkles />} label={selectedSession.mcp_client_name} />
) : null}
<MetaBadge icon={<IconBolt />} label={`${calls} tool call${calls === 1 ? '' : 's'}`} />
<MetaBadge icon={<IconClock />} label={formatDuration(durationMs)} />
<CopyToClipboardInline
explicitValue={selectedSession.session_id}
description="session id"
iconSize="xsmall"
tooltipMessage={<IdTooltip label="Session ID" value={selectedSession.session_id} />}
className="rounded-full bg-surface-secondary px-2 py-0.5 text-[11px] text-secondary font-mono"
>
{shortenSessionId(selectedSession.session_id)}
</CopyToClipboardInline>
</>
)}
</div>
</header>
<section className="flex-1 min-h-0 overflow-y-auto px-3 py-3">
{loading ? (
<div className="flex flex-col gap-2">
<LemonSkeleton repeat={3} className="h-16 w-full" />
</div>
) : error ? (
<div className="flex flex-col items-start gap-2 text-sm text-secondary">
Could not load this session's tool calls.
<LemonButton size="small" onClick={() => loadToolCalls(selectedSession.session_id)}>
Retry
</LemonButton>
</div>
) : toolCalls.length === 0 ? (
<div className="text-sm text-secondary">No tool calls captured for this session.</div>
) : (
<>
<ol className="flex flex-col gap-2 list-none pl-0">
{toolCalls.map((toolCall, idx) => (
<li
key={toolCall.event_id || `${toolCall.timestamp}-${idx}`}
className={`relative flex flex-col gap-1 rounded border bg-surface-primary px-2 py-1.5 transition-colors hover:bg-surface-secondary ${
toolCall.is_error
? 'border-danger border-l-2'
: 'border-primary border-l-2 border-l-accent/60'
}`}
>
<div className="flex items-center justify-between gap-2 text-xs">
<div className="flex items-center gap-1.5 min-w-0">
{toolCall.is_error ? (
<IconWarning className="text-danger shrink-0 text-xs" />
) : (
<IconBolt className="text-accent shrink-0 text-xs" />
)}
<span className="font-mono font-semibold truncate">
{toolCall.tool_name || 'unknown'}
</span>
{toolCall.is_error ? (
<LemonTag type="danger" size="small">
error
</LemonTag>
) : null}
</div>
<div className="flex items-center gap-2 text-secondary whitespace-nowrap font-mono text-[11px]">
{toolCall.duration_ms != null ? (
<span>{formatDuration(toolCall.duration_ms)} ·</span>
) : null}
<span>
{formatRelativeOffset(
selectedSession.session_start,
toolCall.timestamp
)}
</span>
</div>
</div>
<div className="text-xs leading-snug pl-5">
{toolCall.intent || (
<span className="text-secondary italic">No intent captured.</span>
)}
</div>
{toolCall.is_error && toolCall.error_message ? (
<div className="text-xs text-danger font-mono pl-5 break-all">
{toolCall.error_message}
</div>
) : null}
</li>
))}
</ol>
{hasNext ? (
<div className="flex justify-center pt-2" data-quill>
<Button
variant="outline"
size="sm"
onClick={() => loadMoreToolCalls()}
disabled={loadingMore}
data-attr="mcp-session-load-more-tool-calls"
>
{loadingMore ? <Spinner /> : null}
Load more
</Button>
</div>
) : null}
</>
)}
</section>
<hr className="shrink-0 border-t border-primary my-0" />
<footer className="shrink-0 bg-gradient-to-br from-accent/15 via-accent/5 to-surface-primary px-3 py-3">
{loading ? (
<LemonSkeleton className="h-7 w-48" />
) : selectedSessionIntent ? (
<>
<div className="flex items-center gap-2 mb-1.5">
<div className="flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-accent/20 text-accent">
<IconSparkles className="text-xs" />
</div>
<span className="text-[10px] uppercase tracking-wider font-semibold text-accent">
Session intent
</span>
</div>
<p className="text-xs leading-relaxed text-default">{selectedSessionIntent}</p>
</>
) : (
<AccessControlAction
resourceType={AccessControlResourceType.McpAnalytics}
minAccessLevel={AccessControlLevel.Editor}
>
<LemonButton
type="primary"
size="xsmall"
icon={<IconSparkles />}
loading={isSelectedSessionGenerating}
onClick={() => generateIntent(selectedSession.session_id)}
>
{isSelectedSessionGenerating ? 'Thinking…' : 'Summarize session intent'}
</LemonButton>
</AccessControlAction>
)}
</footer>
{!loading && toolCalls.length > 0 && (
<MCPAnalyticsFeedbackPrompt
contextKey={selectedSession.session_id}
prompt={MCP_ANALYTICS_SESSION_FEEDBACK_PROMPT}
/>
)}
</div>
)
}