"use client"; import { useCopilotAction, useCopilotChat } from "@copilotkit/react-core"; import { TextMessage, Role } from "@copilotkit/runtime-client-gql"; export type McpTestPrompt = { label: string; message: string }; function parsePrompts(raw: unknown): McpTestPrompt[] { if (typeof raw !== "string" || !raw.trim()) return []; try { const parsed = JSON.parse(raw) as unknown; if (!Array.isArray(parsed)) return []; const out: McpTestPrompt[] = []; for (const item of parsed) { if (!item || typeof item !== "object") continue; const o = item as Record; const label = o.label; const message = o.message; if (typeof label !== "string" || typeof message !== "string") continue; const l = label.trim(); const m = message.trim(); if (!l || !m) continue; out.push({ label: l, message: m }); } return out.slice(0, 8); } catch { return []; } } function TestPromptButtons({ prompts }: { prompts: McpTestPrompt[] }) { const { appendMessage } = useCopilotChat(); if (prompts.length === 0) return null; return (

Try the MCP server

{prompts.map((p, i) => ( ))}
); } /** * Registers a frontend-only CopilotKit action: the agent calls `show_mcp_test_prompts` * with a JSON string of `{ label, message }[]`; the chat renders chips that append * the message in the same thread when clicked (Option C). */ export function RegisterMcpTestPromptsAction() { useCopilotAction({ name: "show_mcp_test_prompts", description: "Show clickable test prompts in the chat so the user can try the connected MCP server. " + "Call after refresh_mcp_tools when tools are visible. " + 'Pass prompts_json: a JSON array string like [{"label":"List tools","message":"List all tools on the MCP server"}].', parameters: [ { name: "prompts_json", type: "string", description: 'JSON array of objects with "label" (short chip) and "message" (full text sent on click). Max ~8 items.', required: true, }, ], handler: async ({ prompts_json }) => { const n = parsePrompts(prompts_json).length; return n > 0 ? `Showing ${n} test prompt chip(s). The user can click one to send it in chat.` : "No valid prompts in prompts_json — use a JSON array of {label, message}."; }, render: ({ args }) => { const prompts = parsePrompts(args?.prompts_json); if (prompts.length === 0) { return (

Could not parse test prompts. Pass prompts_json as a JSON array of objects with label and message fields.

); } return ; }, }); return null; }