Open History to see the last commit
1/**
2 * /api/mcp-status — connectivity + capability probe for the demo's MCP servers.
3 *
4 * Hit `GET /api/mcp-status` (optionally `?server=everything`) to get a JSON
5 * snapshot of each keyless reference server: whether it connects, and the
6 * tools / resources / prompts it exposes. Handy for validating MCP wiring from
7 * the client without driving a full chat turn.
8 *
9 * Each server is probed with a fresh single-use client that is closed before
10 * responding — this endpoint never keeps connections warm.
11 */
12import { createFileRoute } from '@tanstack/react-router'
13import { createMCPClient } from '@tanstack/ai-mcp'
14import type { Transport } from '@tanstack/ai-mcp'
15import {
16 everythingTransport,
17 memoryTransport,
18 sequentialThinkingTransport,
19} from '@/lib/mcp-servers'
20
21const SERVERS: Array<{ name: string; transport: () => Transport }> = [
22 { name: 'everything', transport: everythingTransport },
23 { name: 'memory', transport: memoryTransport },
24 { name: 'thinking', transport: sequentialThinkingTransport },
25]
26
27interface ServerStatus {
28 name: string
29 connected: boolean
30 tools: Array<string>
31 resources: Array<string>
32 prompts: Array<string>
33 error?: string
34}
35
36async function probe(
37 name: string,
38 makeTransport: () => Transport,
39): Promise<ServerStatus> {
40 try {
41 const client = await createMCPClient({ transport: makeTransport() })
42 try {
43 const tools = (await client.tools()).map((t) => t.name)
44 // Check the advertised capabilities instead of catch-all-ing the list
45 // calls: a server without the capability reports "none", while a real
46 // transport/protocol failure propagates to the outer catch and is
47 // surfaced as a probe error (not silently collapsed to []).
48 const resources = client.capabilities.resources
49 ? (await client.resources()).map((x) => x.uri)
50 : []
51 const prompts = client.capabilities.prompts
52 ? (await client.prompts()).map((x) => x.name)
53 : []
54 return { name, connected: true, tools, resources, prompts }
55 } finally {
56 await client.close()
57 }
58 } catch (error) {
59 return {
60 name,
61 connected: false,
62 tools: [],
63 resources: [],
64 prompts: [],
65 error: error instanceof Error ? error.message : String(error),
66 }
67 }
68}
69
70export const Route = createFileRoute('/api/mcp-status')({
71 server: {
72 handlers: {
73 GET: async ({ request }) => {
74 const only = new URL(request.url).searchParams.get('server')
75 const targets = only ? SERVERS.filter((s) => s.name === only) : SERVERS
76 const servers = await Promise.all(
77 targets.map((s) => probe(s.name, s.transport)),
78 )
79 return Response.json({ servers })
80 },
81 },
82 },
83})