Skip to content

Commit 16ebf1b

Browse files
committed
Cleans up return type
1 parent ebab9c0 commit 16ebf1b

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

src/index.ts

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { McpAgent } from "agents/mcp";
22
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
33
import { z } from "zod";
44
import OpenAI from "openai";
5-
import { openai } from '@ai-sdk/openai';
6-
import { generateObject } from 'ai';
75
import { zodResponseFormat } from "openai/helpers/zod.mjs";
86

97

@@ -16,24 +14,21 @@ interface InkeepEnv extends Env {
1614
INKEEP_PRODUCT_NAME: string;
1715
}
1816

19-
// Interface matching the Python response structure
17+
// https://docs.inkeep.com/ai-api/rag-mode/openai-sdk
2018
const InkeepRAGDocumentSchema = z.object({
2119
// anthropic fields citation types
2220
type: z.string(),
2321
source: z.record(z.any()),
24-
title: z.string().optional().nullable(),
25-
context: z.string().optional().nullable(),
22+
title: z.string().optional(),
23+
context: z.string().optional(),
2624
// inkeep specific fields
27-
record_type: z.string().optional().nullable(),
28-
url: z.string().optional().nullable(),
29-
});
25+
record_type: z.string().optional(),
26+
url: z.string().optional(),
27+
}).passthrough();
3028

3129
const InkeepRAGResponseSchema = z.object({
3230
content: z.array(InkeepRAGDocumentSchema),
33-
});
34-
35-
type InkeepRAGDocument = z.infer<typeof InkeepRAGDocumentSchema>;
36-
type InkeepRAGResponse = z.infer<typeof InkeepRAGResponseSchema>;
31+
}).passthrough();
3732

3833
// Define our MCP agent with tools
3934
export class MyMCP extends McpAgent {
@@ -74,7 +69,6 @@ export class MyMCP extends McpAgent {
7469
// Retrieve settings from environment variables
7570
const apiBaseUrl = MyMCP.env.INKEEP_API_BASE_URL || "https://api.inkeep.com/v1";
7671
const apiKey = MyMCP.env.INKEEP_API_KEY;
77-
const apiModel = "inkeep-rag";
7872

7973
if (!apiKey) {
8074
console.error("Inkeep API key not provided");
@@ -92,37 +86,20 @@ export class MyMCP extends McpAgent {
9286
response_format: zodResponseFormat(InkeepRAGResponseSchema, "InkeepRAGResponseSchema"),
9387
});
9488

95-
// Transform InkeepRAGDocuments to MCP-compatible format
96-
const formattedContent = response.choices[0].message.parsed?.content.map(doc => {
97-
// Extract text from the source when possible
98-
let text = "";
99-
100-
if (typeof doc.source === "object" && doc.source) {
101-
// Try to extract text from common source formats
102-
if (Array.isArray(doc.source.content) && doc.source.content.length > 0) {
103-
// Join all text content if available
104-
text = doc.source.content
105-
.filter(item => item.text)
106-
.map(item => item.text)
107-
.join("\n");
108-
} else if (typeof doc.source.text === "string") {
109-
text = doc.source.text;
110-
} else {
111-
// Fallback to stringifying the source
112-
text = JSON.stringify(doc.source);
113-
}
114-
} else {
115-
text = JSON.stringify(doc.source);
116-
}
117-
118-
// Return a properly formatted text item for MCP
89+
const parsedResponse = response.choices[0].message.parsed
90+
if (parsedResponse) {
11991
return {
120-
type: "text" as const,
121-
text: text || "No content available",
92+
content: [
93+
{
94+
type: "text" as const,
95+
text: JSON.stringify(parsedResponse),
96+
},
97+
],
12298
};
123-
}) || [];
99+
}
124100

125-
return { content: formattedContent };
101+
// If no response, return empty array
102+
return { content: [] };
126103
} catch (error) {
127104
console.error("Error retrieving product docs:", error);
128105
return { content: [] };
@@ -143,21 +120,20 @@ export class MyMCP extends McpAgent {
143120
// Retrieve settings from environment variables
144121
const apiBaseUrl = MyMCP.env.INKEEP_API_BASE_URL || "https://api.inkeep.com/v1";
145122
const apiKey = MyMCP.env.INKEEP_API_KEY;
146-
const apiModel = "inkeep-qa-expert";
147123

148124
if (!apiKey) {
149125
console.error("Inkeep API key not provided");
150126
return { content: [] };
151127
}
152128

153-
// Create OpenAI client fromSDK with Inkeep API settings
129+
// Create OpenAI client with Inkeep API settings
154130
const openai = new OpenAI({
155131
baseURL: apiBaseUrl,
156132
apiKey: apiKey,
157133
});
158-
134+
159135
const response = await openai.chat.completions.create({
160-
model: apiModel,
136+
model: "inkeep-qa-expert",
161137
messages: [
162138
{ role: "user", content: question },
163139
],

0 commit comments

Comments
 (0)