Skip to content

Commit 362f1ff

Browse files
committed
Fix AI chat request format, add error handling
1 parent f06554d commit 362f1ff

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import { randomUUID } from "crypto";
2+
13
const BASE_URL = "https://buildwithfern.com/learn";
4+
const X_FERN_HOST = "buildwithfern.com";
25

36
export async function postChat(message: string) {
47
const response = await fetch(`${BASE_URL}/api/fern-docs/search/v2/chat`, {
58
method: "POST",
69
headers: {
710
"content-type": "application/json",
11+
"x-fern-host": X_FERN_HOST,
812
},
913
body: JSON.stringify({
10-
messages: [{ role: "user", content: message }],
11-
url: "https://buildwithfern.com/learn",
12-
filters: [],
14+
messages: [{ role: "user", parts: [{ type: "text", text: message }] }],
15+
conversationId: randomUUID(),
16+
url: BASE_URL,
1317
}),
1418
});
1519

src/mcp.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ export function registerMcpTools(server: McpServer) {
2525
"Ask Fern AI about anything related to Fern.",
2626
{ message: z.string() },
2727
async ({ message }) => {
28-
const result = await api.postChat(message);
29-
return {
30-
content: [{ type: "text", text: result }],
31-
};
28+
try {
29+
const result = await api.postChat(message);
30+
return {
31+
content: [{ type: "text", text: result }],
32+
};
33+
} catch (error) {
34+
return {
35+
content: [{ type: "text", text: "[ERROR] " + error.message }],
36+
isError: true,
37+
};
38+
}
3239
}
3340
);
3441
}

0 commit comments

Comments
 (0)