@@ -2,8 +2,6 @@ import { McpAgent } from "agents/mcp";
2
2
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
3
3
import { z } from "zod" ;
4
4
import OpenAI from "openai" ;
5
- import { openai } from '@ai-sdk/openai' ;
6
- import { generateObject } from 'ai' ;
7
5
import { zodResponseFormat } from "openai/helpers/zod.mjs" ;
8
6
9
7
@@ -16,24 +14,21 @@ interface InkeepEnv extends Env {
16
14
INKEEP_PRODUCT_NAME : string ;
17
15
}
18
16
19
- // Interface matching the Python response structure
17
+ // https://docs.inkeep.com/ai-api/rag-mode/openai-sdk
20
18
const InkeepRAGDocumentSchema = z . object ( {
21
19
// anthropic fields citation types
22
20
type : z . string ( ) ,
23
21
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 ( ) ,
26
24
// 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 ( ) ;
30
28
31
29
const InkeepRAGResponseSchema = z . object ( {
32
30
content : z . array ( InkeepRAGDocumentSchema ) ,
33
- } ) ;
34
-
35
- type InkeepRAGDocument = z . infer < typeof InkeepRAGDocumentSchema > ;
36
- type InkeepRAGResponse = z . infer < typeof InkeepRAGResponseSchema > ;
31
+ } ) . passthrough ( ) ;
37
32
38
33
// Define our MCP agent with tools
39
34
export class MyMCP extends McpAgent {
@@ -74,7 +69,6 @@ export class MyMCP extends McpAgent {
74
69
// Retrieve settings from environment variables
75
70
const apiBaseUrl = MyMCP . env . INKEEP_API_BASE_URL || "https://api.inkeep.com/v1" ;
76
71
const apiKey = MyMCP . env . INKEEP_API_KEY ;
77
- const apiModel = "inkeep-rag" ;
78
72
79
73
if ( ! apiKey ) {
80
74
console . error ( "Inkeep API key not provided" ) ;
@@ -92,37 +86,20 @@ export class MyMCP extends McpAgent {
92
86
response_format : zodResponseFormat ( InkeepRAGResponseSchema , "InkeepRAGResponseSchema" ) ,
93
87
} ) ;
94
88
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 ) {
119
91
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
+ ] ,
122
98
} ;
123
- } ) || [ ] ;
99
+ }
124
100
125
- return { content : formattedContent } ;
101
+ // If no response, return empty array
102
+ return { content : [ ] } ;
126
103
} catch ( error ) {
127
104
console . error ( "Error retrieving product docs:" , error ) ;
128
105
return { content : [ ] } ;
@@ -143,21 +120,20 @@ export class MyMCP extends McpAgent {
143
120
// Retrieve settings from environment variables
144
121
const apiBaseUrl = MyMCP . env . INKEEP_API_BASE_URL || "https://api.inkeep.com/v1" ;
145
122
const apiKey = MyMCP . env . INKEEP_API_KEY ;
146
- const apiModel = "inkeep-qa-expert" ;
147
123
148
124
if ( ! apiKey ) {
149
125
console . error ( "Inkeep API key not provided" ) ;
150
126
return { content : [ ] } ;
151
127
}
152
128
153
- // Create OpenAI client fromSDK with Inkeep API settings
129
+ // Create OpenAI client with Inkeep API settings
154
130
const openai = new OpenAI ( {
155
131
baseURL : apiBaseUrl ,
156
132
apiKey : apiKey ,
157
133
} ) ;
158
-
134
+
159
135
const response = await openai . chat . completions . create ( {
160
- model : apiModel ,
136
+ model : "inkeep-qa-expert" ,
161
137
messages : [
162
138
{ role : "user" , content : question } ,
163
139
] ,
0 commit comments