|
1 | | -import { getEnvVariable, getNodeEnvironment } from "@stackframe/stack-shared/dist/utils/env"; |
| 1 | +import { createMCPClient, type MCPClient } from "@ai-sdk/mcp"; |
| 2 | +import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; |
2 | 3 | import { captureError } from "@stackframe/stack-shared/dist/utils/errors"; |
3 | 4 | import { tool } from "ai"; |
4 | 5 | import { z } from "zod"; |
5 | 6 |
|
6 | | -type DocsToolHttpResult = { |
7 | | - content?: Array<{ type: string, text?: string }>, |
8 | | - isError?: boolean, |
9 | | -}; |
| 7 | +let mintlifyMcpClientPromise: Promise<MCPClient> | null = null; |
10 | 8 |
|
11 | | -function getDocsToolsBaseUrl(): string { |
12 | | - const fromEnv = getEnvVariable("STACK_DOCS_INTERNAL_BASE_URL", ""); |
13 | | - if (fromEnv !== "") { |
14 | | - return fromEnv.replace(/\/$/, ""); |
15 | | - } |
16 | | - if (getNodeEnvironment() === "development") { |
17 | | - const portPrefix = getEnvVariable("NEXT_PUBLIC_STACK_PORT_PREFIX", "81"); |
18 | | - return `http://localhost:${portPrefix}26`; |
19 | | - } |
20 | | - return "https://mcp.stack-auth.com"; |
| 9 | +function getMintlifyMcpUrl(): string { |
| 10 | + return getEnvVariable("STACK_MINTLIFY_MCP_URL", "https://stackauth-e0affa27.mintlify.app/mcp"); |
21 | 11 | } |
22 | 12 |
|
23 | | -async function postDocsToolAction(action: Record<string, unknown>): Promise<string> { |
24 | | - const base = getDocsToolsBaseUrl(); |
25 | | - try { |
26 | | - const res = await fetch(`${base}/api/internal/docs-tools`, { |
27 | | - method: "POST", |
28 | | - headers: { |
29 | | - "Content-Type": "application/json", |
30 | | - // MCP-style JSON-RPC endpoint requires clients to advertise both JSON and SSE. |
31 | | - Accept: "application/json, text/event-stream", |
| 13 | +async function getMintlifyMcpClient(): Promise<MCPClient> { |
| 14 | + if (mintlifyMcpClientPromise == null) { |
| 15 | + mintlifyMcpClientPromise = createMCPClient({ |
| 16 | + transport: { |
| 17 | + type: "http", |
| 18 | + url: getMintlifyMcpUrl(), |
32 | 19 | }, |
33 | | - body: JSON.stringify(action), |
| 20 | + name: "stack-auth-backend-docs-agent", |
| 21 | + }).catch((err: unknown) => { |
| 22 | + mintlifyMcpClientPromise = null; |
| 23 | + throw err; |
34 | 24 | }); |
35 | | - |
36 | | - if (!res.ok) { |
37 | | - const errBody = await res.text(); |
38 | | - captureError("docs-tools-http-error", new Error(`Stack Auth docs tools error (${res.status}): ${errBody}`)); |
39 | | - return "Stack Auth docs tools returned an error. Please try again later."; |
40 | | - } |
41 | | - |
42 | | - const data = (await res.json()) as DocsToolHttpResult; |
43 | | - const text = data.content |
44 | | - ?.filter((c): c is { type: "text", text: string } => c.type === "text" && typeof c.text === "string") |
45 | | - .map((c) => c.text) |
46 | | - .join("\n") ?? ""; |
47 | | - |
48 | | - if (data.isError === true) { |
49 | | - return text || "Unknown docs tool error"; |
50 | | - } |
51 | | - |
52 | | - return text; |
53 | | - } catch (err) { |
54 | | - captureError("docs-tools-transport-error", err instanceof Error ? err : new Error(String(err))); |
55 | | - return "Stack Auth docs tools are temporarily unavailable. Please try again later."; |
56 | 25 | } |
| 26 | + |
| 27 | + return await mintlifyMcpClientPromise; |
57 | 28 | } |
58 | 29 |
|
59 | 30 | /** |
60 | | - * Documentation tools backed by the docs app's `/api/internal/docs-tools` endpoint. |
61 | | - * |
62 | | - * The public MCP server at the same docs origin exposes only `ask_stack_auth`, which proxies to |
63 | | - * `/api/latest/ai/query/generate`; these tools avoid MCP recursion by calling the HTTP API directly. |
| 31 | + * Documentation tools backed by Mintlify's generated MCP server. |
| 32 | + * The public Stack Auth MCP server still exposes the higher-level `ask_stack_auth` tool; |
| 33 | + * that agent uses these lower-level Mintlify tools for search and page reads. |
64 | 34 | */ |
65 | 35 | export async function createDocsTools() { |
66 | | - return { |
67 | | - list_available_docs: tool({ |
68 | | - description: |
69 | | - "Use this tool to learn about what Stack Auth is, available documentation, and see if you can use it for what you're working on. It returns a list of all available Stack Auth Documentation pages.", |
70 | | - inputSchema: z.object({}), |
71 | | - execute: async () => { |
72 | | - return await postDocsToolAction({ action: "list_available_docs" }); |
73 | | - }, |
74 | | - }), |
75 | | - |
76 | | - search_docs: tool({ |
77 | | - description: |
78 | | - "Search through all Stack Auth documentation including API docs, guides, and examples. Returns ranked results with snippets and relevance scores.", |
79 | | - inputSchema: z.object({ |
80 | | - search_query: z.string().describe("The search query to find relevant documentation"), |
81 | | - result_limit: z.number().optional().describe("Maximum number of results to return (default: 50)"), |
82 | | - }), |
83 | | - execute: async ({ search_query, result_limit = 50 }) => { |
84 | | - return await postDocsToolAction({ |
85 | | - action: "search_docs", |
86 | | - search_query, |
87 | | - result_limit, |
88 | | - }); |
89 | | - }, |
90 | | - }), |
91 | | - |
92 | | - get_docs_by_id: tool({ |
93 | | - description: |
94 | | - "Use this tool to retrieve a specific Stack Auth Documentation page by its ID. It gives you the full content of the page so you can know exactly how to use specific Stack Auth APIs. Whenever using Stack Auth, you should always check the documentation first to have the most up-to-date information. When you write code using Stack Auth documentation you should reference the content you used in your comments.", |
95 | | - inputSchema: z.object({ |
96 | | - id: z.string(), |
97 | | - }), |
98 | | - execute: async ({ id }) => { |
99 | | - return await postDocsToolAction({ action: "get_docs_by_id", id }); |
100 | | - }, |
101 | | - }), |
102 | | - |
103 | | - get_stack_auth_setup_instructions: tool({ |
104 | | - description: |
105 | | - "Use this tool when the user wants to set up authentication in a new project. It provides step-by-step instructions for installing and configuring Stack Auth authentication.", |
106 | | - inputSchema: z.object({}), |
107 | | - execute: async () => { |
108 | | - return await postDocsToolAction({ action: "get_stack_auth_setup_instructions" }); |
109 | | - }, |
110 | | - }), |
111 | | - |
112 | | - search: tool({ |
113 | | - description: |
114 | | - "Search for Stack Auth documentation pages.\n\nUse this tool to find documentation pages that contain a specific keyword or phrase.", |
115 | | - inputSchema: z.object({ |
116 | | - query: z.string(), |
117 | | - }), |
118 | | - execute: async ({ query }) => { |
119 | | - return await postDocsToolAction({ action: "search", query }); |
120 | | - }, |
121 | | - }), |
122 | | - |
123 | | - fetch: tool({ |
124 | | - description: |
125 | | - "Fetch a particular Stack Auth Documentation page by its ID.\n\nThis tool is identical to `get_docs_by_id`.", |
126 | | - inputSchema: z.object({ |
127 | | - id: z.string(), |
| 36 | + try { |
| 37 | + const client = await getMintlifyMcpClient(); |
| 38 | + return await client.tools(); |
| 39 | + } catch (error) { |
| 40 | + captureError("mintlify-mcp-docs-tools", error); |
| 41 | + return { |
| 42 | + docsUnavailable: tool({ |
| 43 | + description: "Report that the Stack Auth documentation search tools are currently unavailable.", |
| 44 | + inputSchema: z.object({}), |
| 45 | + execute: async () => ({ |
| 46 | + error: "Stack Auth documentation search is temporarily unavailable. Please try again later.", |
| 47 | + }), |
128 | 48 | }), |
129 | | - execute: async ({ id }) => { |
130 | | - return await postDocsToolAction({ action: "fetch", id }); |
131 | | - }, |
132 | | - }), |
133 | | - }; |
| 49 | + }; |
| 50 | + } |
134 | 51 | } |
0 commit comments