@inkeep/agents-run-api 0.1.3 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/LICENSE.md +7 -0
  2. package/SUPPLEMENTAL_TERMS.md +40 -0
  3. package/dist/chunk-P6IQZWFC.js +239 -0
  4. package/dist/conversations-EUPRCMQZ.js +1 -0
  5. package/dist/index.cjs +8992 -0
  6. package/dist/index.d.cts +15 -0
  7. package/dist/index.d.ts +13 -21
  8. package/dist/index.js +8644 -27
  9. package/package.json +8 -7
  10. package/dist/AgentExecutionServer.d.ts +0 -28
  11. package/dist/AgentExecutionServer.d.ts.map +0 -1
  12. package/dist/AgentExecutionServer.js +0 -41
  13. package/dist/__tests__/setup.d.ts +0 -4
  14. package/dist/__tests__/setup.d.ts.map +0 -1
  15. package/dist/__tests__/setup.js +0 -80
  16. package/dist/__tests__/utils/testProject.d.ts +0 -18
  17. package/dist/__tests__/utils/testProject.d.ts.map +0 -1
  18. package/dist/__tests__/utils/testProject.js +0 -26
  19. package/dist/__tests__/utils/testRequest.d.ts +0 -8
  20. package/dist/__tests__/utils/testRequest.d.ts.map +0 -1
  21. package/dist/__tests__/utils/testRequest.js +0 -32
  22. package/dist/__tests__/utils/testTenant.d.ts +0 -64
  23. package/dist/__tests__/utils/testTenant.d.ts.map +0 -1
  24. package/dist/__tests__/utils/testTenant.js +0 -71
  25. package/dist/a2a/client.d.ts +0 -182
  26. package/dist/a2a/client.d.ts.map +0 -1
  27. package/dist/a2a/client.js +0 -645
  28. package/dist/a2a/handlers.d.ts +0 -4
  29. package/dist/a2a/handlers.d.ts.map +0 -1
  30. package/dist/a2a/handlers.js +0 -656
  31. package/dist/a2a/transfer.d.ts +0 -18
  32. package/dist/a2a/transfer.d.ts.map +0 -1
  33. package/dist/a2a/transfer.js +0 -22
  34. package/dist/a2a/types.d.ts +0 -63
  35. package/dist/a2a/types.d.ts.map +0 -1
  36. package/dist/a2a/types.js +0 -1
  37. package/dist/agents/Agent.d.ts +0 -154
  38. package/dist/agents/Agent.d.ts.map +0 -1
  39. package/dist/agents/Agent.js +0 -1110
  40. package/dist/agents/ModelFactory.d.ts +0 -62
  41. package/dist/agents/ModelFactory.d.ts.map +0 -1
  42. package/dist/agents/ModelFactory.js +0 -208
  43. package/dist/agents/SystemPromptBuilder.d.ts +0 -14
  44. package/dist/agents/SystemPromptBuilder.d.ts.map +0 -1
  45. package/dist/agents/SystemPromptBuilder.js +0 -62
  46. package/dist/agents/ToolSessionManager.d.ts +0 -61
  47. package/dist/agents/ToolSessionManager.d.ts.map +0 -1
  48. package/dist/agents/ToolSessionManager.js +0 -143
  49. package/dist/agents/artifactTools.d.ts +0 -30
  50. package/dist/agents/artifactTools.d.ts.map +0 -1
  51. package/dist/agents/artifactTools.js +0 -463
  52. package/dist/agents/generateTaskHandler.d.ts +0 -41
  53. package/dist/agents/generateTaskHandler.d.ts.map +0 -1
  54. package/dist/agents/generateTaskHandler.js +0 -350
  55. package/dist/agents/relationTools.d.ts +0 -35
  56. package/dist/agents/relationTools.d.ts.map +0 -1
  57. package/dist/agents/relationTools.js +0 -246
  58. package/dist/agents/types.d.ts +0 -23
  59. package/dist/agents/types.d.ts.map +0 -1
  60. package/dist/agents/types.js +0 -1
  61. package/dist/agents/versions/V1Config.d.ts +0 -21
  62. package/dist/agents/versions/V1Config.d.ts.map +0 -1
  63. package/dist/agents/versions/V1Config.js +0 -285
  64. package/dist/app.d.ts +0 -12
  65. package/dist/app.d.ts.map +0 -1
  66. package/dist/app.js +0 -216
  67. package/dist/data/agentGraph.d.ts +0 -4
  68. package/dist/data/agentGraph.d.ts.map +0 -1
  69. package/dist/data/agentGraph.js +0 -73
  70. package/dist/data/agents.d.ts +0 -4
  71. package/dist/data/agents.d.ts.map +0 -1
  72. package/dist/data/agents.js +0 -78
  73. package/dist/data/conversations.d.ts +0 -59
  74. package/dist/data/conversations.d.ts.map +0 -1
  75. package/dist/data/conversations.js +0 -216
  76. package/dist/data/db/clean.d.ts +0 -6
  77. package/dist/data/db/clean.d.ts.map +0 -1
  78. package/dist/data/db/clean.js +0 -77
  79. package/dist/data/db/dbClient.d.ts +0 -3
  80. package/dist/data/db/dbClient.d.ts.map +0 -1
  81. package/dist/data/db/dbClient.js +0 -13
  82. package/dist/env.d.ts +0 -45
  83. package/dist/env.d.ts.map +0 -1
  84. package/dist/env.js +0 -64
  85. package/dist/handlers/executionHandler.d.ts +0 -36
  86. package/dist/handlers/executionHandler.d.ts.map +0 -1
  87. package/dist/handlers/executionHandler.js +0 -399
  88. package/dist/index.d.ts.map +0 -1
  89. package/dist/instrumentation.d.ts +0 -13
  90. package/dist/instrumentation.d.ts.map +0 -1
  91. package/dist/instrumentation.js +0 -66
  92. package/dist/logger.d.ts +0 -4
  93. package/dist/logger.d.ts.map +0 -1
  94. package/dist/logger.js +0 -32
  95. package/dist/middleware/api-key-auth.d.ts +0 -22
  96. package/dist/middleware/api-key-auth.d.ts.map +0 -1
  97. package/dist/middleware/api-key-auth.js +0 -139
  98. package/dist/middleware/index.d.ts +0 -2
  99. package/dist/middleware/index.d.ts.map +0 -1
  100. package/dist/middleware/index.js +0 -1
  101. package/dist/openapi.d.ts +0 -2
  102. package/dist/openapi.d.ts.map +0 -1
  103. package/dist/openapi.js +0 -36
  104. package/dist/routes/agents.d.ts +0 -10
  105. package/dist/routes/agents.d.ts.map +0 -1
  106. package/dist/routes/agents.js +0 -158
  107. package/dist/routes/chat.d.ts +0 -10
  108. package/dist/routes/chat.d.ts.map +0 -1
  109. package/dist/routes/chat.js +0 -307
  110. package/dist/routes/chatDataStream.d.ts +0 -10
  111. package/dist/routes/chatDataStream.d.ts.map +0 -1
  112. package/dist/routes/chatDataStream.js +0 -179
  113. package/dist/routes/mcp.d.ts +0 -10
  114. package/dist/routes/mcp.d.ts.map +0 -1
  115. package/dist/routes/mcp.js +0 -500
  116. package/dist/server.d.ts +0 -5
  117. package/dist/server.d.ts.map +0 -1
  118. package/dist/server.js +0 -61
  119. package/dist/tracer.d.ts +0 -24
  120. package/dist/tracer.d.ts.map +0 -1
  121. package/dist/tracer.js +0 -107
  122. package/dist/types/chat.d.ts +0 -25
  123. package/dist/types/chat.d.ts.map +0 -1
  124. package/dist/types/chat.js +0 -1
  125. package/dist/types/execution-context.d.ts +0 -14
  126. package/dist/types/execution-context.d.ts.map +0 -1
  127. package/dist/types/execution-context.js +0 -14
  128. package/dist/utils/agent-operations.d.ts +0 -92
  129. package/dist/utils/agent-operations.d.ts.map +0 -1
  130. package/dist/utils/agent-operations.js +0 -78
  131. package/dist/utils/artifact-component-schema.d.ts +0 -29
  132. package/dist/utils/artifact-component-schema.d.ts.map +0 -1
  133. package/dist/utils/artifact-component-schema.js +0 -119
  134. package/dist/utils/artifact-parser.d.ts +0 -71
  135. package/dist/utils/artifact-parser.d.ts.map +0 -1
  136. package/dist/utils/artifact-parser.js +0 -251
  137. package/dist/utils/cleanup.d.ts +0 -19
  138. package/dist/utils/cleanup.d.ts.map +0 -1
  139. package/dist/utils/cleanup.js +0 -66
  140. package/dist/utils/data-component-schema.d.ts +0 -6
  141. package/dist/utils/data-component-schema.d.ts.map +0 -1
  142. package/dist/utils/data-component-schema.js +0 -43
  143. package/dist/utils/graph-session.d.ts +0 -200
  144. package/dist/utils/graph-session.d.ts.map +0 -1
  145. package/dist/utils/graph-session.js +0 -1016
  146. package/dist/utils/incremental-stream-parser.d.ts +0 -57
  147. package/dist/utils/incremental-stream-parser.d.ts.map +0 -1
  148. package/dist/utils/incremental-stream-parser.js +0 -289
  149. package/dist/utils/response-formatter.d.ts +0 -27
  150. package/dist/utils/response-formatter.d.ts.map +0 -1
  151. package/dist/utils/response-formatter.js +0 -160
  152. package/dist/utils/stream-helpers.d.ts +0 -174
  153. package/dist/utils/stream-helpers.d.ts.map +0 -1
  154. package/dist/utils/stream-helpers.js +0 -466
  155. package/dist/utils/stream-registry.d.ts +0 -18
  156. package/dist/utils/stream-registry.d.ts.map +0 -1
  157. package/dist/utils/stream-registry.js +0 -33
@@ -1,500 +0,0 @@
1
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
2
- import { contextValidationMiddleware, HeadersScopeSchema } from '@inkeep/agents-core';
3
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
- import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
5
- import { z } from 'zod/v3';
6
- // Type bridge for MCP SDK compatibility with Zod v4
7
- function createMCPSchema(schema) {
8
- return schema;
9
- }
10
- import { createMessage, createOrGetConversation, getAgentById, getAgentGraphWithDefaultAgent, getConversation, getRequestExecutionContext, handleContextResolution, updateConversation, } from '@inkeep/agents-core';
11
- import { trace } from '@opentelemetry/api';
12
- import { toFetchResponse, toReqRes } from 'fetch-to-node';
13
- import { nanoid } from 'nanoid';
14
- import dbClient from '../data/db/dbClient';
15
- import { ExecutionHandler } from '../handlers/executionHandler';
16
- import { getLogger } from '../logger';
17
- import { createMCPStreamHelper } from '../utils/stream-helpers';
18
- const logger = getLogger('mcp');
19
- /**
20
- * Singleton mock response object for spoof initialization
21
- */
22
- class MockResponseSingleton {
23
- static instance;
24
- mockRes;
25
- constructor() {
26
- // Create the mock response object once
27
- this.mockRes = {
28
- statusCode: 200,
29
- headers: {},
30
- setHeader: function (name, value) {
31
- this.headers[name] = value;
32
- },
33
- getHeaders: function () {
34
- return this.headers;
35
- },
36
- end: () => { },
37
- write: () => { },
38
- writeHead: () => { },
39
- };
40
- }
41
- static getInstance() {
42
- if (!MockResponseSingleton.instance) {
43
- MockResponseSingleton.instance = new MockResponseSingleton();
44
- }
45
- return MockResponseSingleton.instance;
46
- }
47
- getMockResponse() {
48
- // Reset headers for each use to avoid state pollution
49
- this.mockRes.headers = {};
50
- this.mockRes.statusCode = 200;
51
- return this.mockRes;
52
- }
53
- }
54
- /**
55
- * Creates a spoof initialization message with the given protocol version
56
- * Extracted as a pure function for better testability and reuse
57
- */
58
- const createSpoofInitMessage = (mcpProtocolVersion) => ({
59
- method: 'initialize',
60
- params: {
61
- protocolVersion: mcpProtocolVersion || '2025-06-18',
62
- capabilities: {
63
- tools: true,
64
- prompts: true,
65
- resources: false,
66
- logging: false,
67
- roots: { listChanged: false },
68
- },
69
- clientInfo: {
70
- name: 'inkeep-mcp-server',
71
- version: '1.0.0',
72
- },
73
- },
74
- jsonrpc: '2.0',
75
- id: 0,
76
- });
77
- /**
78
- * Spoofs an initialization message to set the transport's initialized flag
79
- * This is necessary when recreating transports for existing sessions because the transport expects to have received an initialization message from the client.
80
- */
81
- const spoofTransportInitialization = async (transport, req, sessionId, mcpProtocolVersion) => {
82
- logger.info({ sessionId }, 'Spoofing initialization message to set transport state');
83
- const spoofInitMessage = createSpoofInitMessage(mcpProtocolVersion);
84
- const mockRes = MockResponseSingleton.getInstance().getMockResponse();
85
- try {
86
- // Send the spoof initialization to set internal state. The transport errors but it still sets the initialized flag.
87
- await transport.handleRequest(req, mockRes, spoofInitMessage);
88
- logger.info({ sessionId }, 'Successfully spoofed initialization');
89
- }
90
- catch (spoofError) {
91
- logger.warn({ sessionId, error: spoofError }, 'Spoof initialization failed, continuing anyway');
92
- }
93
- };
94
- const validateSession = async (req, res, body, tenantId, projectId, graphId) => {
95
- const sessionId = req.headers['mcp-session-id'];
96
- logger.info({ sessionId }, 'Received MCP session ID');
97
- if (!sessionId) {
98
- logger.info({ body }, 'Missing session ID');
99
- res.writeHead(400).end(JSON.stringify({
100
- jsonrpc: '2.0',
101
- error: { code: -32602, message: 'Bad Request: Mcp-Session-Id header is required' },
102
- id: null,
103
- }));
104
- return false;
105
- }
106
- else if (Array.isArray(sessionId)) {
107
- res.writeHead(400).end(JSON.stringify({
108
- jsonrpc: '2.0',
109
- error: {
110
- code: -32000,
111
- message: 'Bad Request: Mcp-Session-Id header must be a single value',
112
- },
113
- id: null,
114
- }));
115
- return false;
116
- }
117
- // Get conversation (which stores our session data)
118
- const conversation = await getConversation(dbClient)({
119
- scopes: { tenantId, projectId },
120
- conversationId: sessionId,
121
- });
122
- // After line 342 - Add logging to debug conversation lookup
123
- logger.info({
124
- sessionId,
125
- conversationFound: !!conversation,
126
- sessionType: conversation?.metadata?.sessionData?.sessionType,
127
- storedGraphId: conversation?.metadata?.sessionData?.graphId,
128
- requestGraphId: graphId,
129
- }, 'Conversation lookup result');
130
- if (!conversation ||
131
- conversation.metadata?.sessionData?.sessionType !== 'mcp' ||
132
- conversation.metadata?.sessionData?.graphId !== graphId) {
133
- logger.info({ sessionId, conversationId: conversation?.id }, 'MCP session not found or invalid');
134
- res.writeHead(404).end(JSON.stringify({
135
- jsonrpc: '2.0',
136
- error: {
137
- code: -32001,
138
- message: 'Session not found',
139
- },
140
- id: null,
141
- }));
142
- return false;
143
- }
144
- return conversation;
145
- };
146
- /**
147
- * Sets up tracing attributes for the active span
148
- */
149
- const setupTracing = (conversationId, tenantId, graphId) => {
150
- const activeSpan = trace.getActiveSpan();
151
- if (activeSpan) {
152
- activeSpan.setAttributes({
153
- 'conversation.id': conversationId,
154
- 'tenant.id': tenantId,
155
- 'graph.id': graphId,
156
- });
157
- }
158
- };
159
- /**
160
- * Processes and stores the user message
161
- */
162
- const processUserMessage = async (tenantId, projectId, conversationId, query) => {
163
- const messageSpan = trace.getActiveSpan();
164
- if (messageSpan) {
165
- messageSpan.setAttributes({
166
- 'message.content': query,
167
- 'message.timestamp': Date.now(),
168
- });
169
- }
170
- await createMessage(dbClient)({
171
- id: nanoid(),
172
- tenantId,
173
- projectId,
174
- conversationId,
175
- role: 'user',
176
- content: {
177
- text: query,
178
- },
179
- visibility: 'user-facing',
180
- messageType: 'chat',
181
- });
182
- };
183
- /**
184
- * Executes the agent query and returns the result
185
- */
186
- const executeAgentQuery = async (executionContext, conversationId, query, defaultAgentId) => {
187
- const requestId = `mcp-${Date.now()}`;
188
- const mcpStreamHelper = createMCPStreamHelper();
189
- const executionHandler = new ExecutionHandler();
190
- const result = await executionHandler.execute({
191
- executionContext,
192
- conversationId,
193
- userMessage: query,
194
- initialAgentId: defaultAgentId,
195
- requestId,
196
- sseHelper: mcpStreamHelper,
197
- });
198
- logger.info({ result }, `Execution completed: ${result.success ? 'success' : 'failed'} after ${result.iterations} iterations`);
199
- if (!result.success) {
200
- return {
201
- content: [
202
- {
203
- type: 'text',
204
- text: result.error ||
205
- `Sorry, I was unable to process your request at this time. Please try again.`,
206
- },
207
- ],
208
- isError: true,
209
- };
210
- }
211
- return {
212
- content: [
213
- {
214
- type: 'text',
215
- text: result.response || 'No response generated',
216
- },
217
- ],
218
- };
219
- };
220
- /**
221
- * Creates and configures an MCP server for the given context
222
- */
223
- const getServer = async (requestContext, executionContext, conversationId, credentialStores) => {
224
- const { tenantId, projectId, graphId } = executionContext;
225
- setupTracing(conversationId, tenantId, graphId);
226
- const agentGraph = await getAgentGraphWithDefaultAgent(dbClient)({
227
- scopes: { tenantId, projectId },
228
- graphId: graphId,
229
- });
230
- if (!agentGraph) {
231
- throw new Error('Agent graph not found');
232
- }
233
- const server = new McpServer({
234
- name: 'inkeep-chat-api-server',
235
- version: '1.0.0',
236
- }, { capabilities: { logging: {} } });
237
- // Register tools and prompts
238
- server.tool('send-query-to-agent', `Send a query to the ${agentGraph.name} agent. The agent has the following description: ${agentGraph.description}`, {
239
- query: createMCPSchema(z.string().describe('The query to send to the agent')),
240
- }, async ({ query }) => {
241
- try {
242
- const defaultAgentId = agentGraph.defaultAgentId;
243
- const agentInfo = await getAgentById(dbClient)({
244
- scopes: { tenantId, projectId },
245
- agentId: defaultAgentId,
246
- });
247
- if (!agentInfo) {
248
- return {
249
- content: [
250
- {
251
- type: 'text',
252
- text: `Agent not found`,
253
- },
254
- ],
255
- isError: true,
256
- };
257
- }
258
- const resolvedContext = await handleContextResolution(tenantId, projectId, conversationId, graphId, requestContext, dbClient, credentialStores);
259
- logger.info({
260
- tenantId,
261
- graphId,
262
- conversationId,
263
- hasContextConfig: !!agentGraph.contextConfigId,
264
- hasRequestContext: !!requestContext,
265
- hasValidatedContext: !!resolvedContext,
266
- }, 'parameters');
267
- await processUserMessage(tenantId, projectId, conversationId, query);
268
- return executeAgentQuery(executionContext, conversationId, query, defaultAgentId);
269
- }
270
- catch (error) {
271
- return {
272
- content: [
273
- {
274
- type: 'text',
275
- text: `Error sending query: ${error instanceof Error ? error.message : 'Unknown error'}`,
276
- },
277
- ],
278
- isError: true,
279
- };
280
- }
281
- });
282
- return server;
283
- };
284
- const app = new OpenAPIHono();
285
- // Only apply context validation to POST requests (GET requests are for SSE streams)
286
- app.use('/', async (c, next) => {
287
- if (c.req.method === 'POST') {
288
- return contextValidationMiddleware(dbClient)(c, next);
289
- }
290
- return next();
291
- });
292
- /**
293
- * Validates request parameters and returns execution context if valid
294
- */
295
- const validateRequestParameters = (c) => {
296
- try {
297
- const executionContext = getRequestExecutionContext(c);
298
- const { tenantId, projectId, graphId } = executionContext;
299
- getLogger('mcp').debug({ tenantId, projectId, graphId }, 'Extracted MCP entity parameters');
300
- return { valid: true, executionContext };
301
- }
302
- catch (error) {
303
- getLogger('chat').warn({ error: error instanceof Error ? error.message : 'Unknown error' }, 'Failed to get execution context');
304
- return {
305
- valid: false,
306
- response: c.json({
307
- jsonrpc: '2.0',
308
- error: { code: -32602, message: 'API key authentication required' },
309
- id: null,
310
- }, { status: 401 }),
311
- };
312
- }
313
- };
314
- /**
315
- * Creates a new MCP session and handles initialization
316
- */
317
- const handleInitializationRequest = async (body, executionContext, validatedContext, req, res, c, credentialStores) => {
318
- const { tenantId, projectId, graphId } = executionContext;
319
- logger.info({ body }, 'Received initialization request');
320
- const sessionId = nanoid();
321
- // Get the default agent for the graph
322
- const agentGraph = await getAgentGraphWithDefaultAgent(dbClient)({
323
- scopes: { tenantId, projectId },
324
- graphId,
325
- });
326
- if (!agentGraph) {
327
- return c.json({
328
- jsonrpc: '2.0',
329
- error: { code: -32001, message: 'Agent graph not found' },
330
- id: body.id || null,
331
- }, { status: 404 });
332
- }
333
- // Create/get conversation with MCP session metadata
334
- const conversation = await createOrGetConversation(dbClient)({
335
- id: sessionId,
336
- tenantId,
337
- projectId,
338
- activeAgentId: agentGraph.defaultAgentId,
339
- metadata: {
340
- sessionData: {
341
- graphId,
342
- sessionType: 'mcp',
343
- mcpProtocolVersion: c.req.header('mcp-protocol-version'),
344
- initialized: false, // Track initialization state
345
- },
346
- },
347
- });
348
- logger.info({ sessionId, conversationId: conversation.id }, 'Created MCP session as conversation');
349
- // Create fresh transport and server for this request
350
- const transport = new StreamableHTTPServerTransport({
351
- sessionIdGenerator: () => sessionId,
352
- });
353
- const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
354
- await server.connect(transport);
355
- logger.info({ sessionId }, 'Server connected for initialization');
356
- // Tell client the session ID
357
- res.setHeader('Mcp-Session-Id', sessionId);
358
- logger.info({
359
- sessionId,
360
- bodyMethod: body?.method,
361
- bodyId: body?.id,
362
- }, 'About to handle initialization request');
363
- await transport.handleRequest(req, res, body);
364
- logger.info({ sessionId }, 'Successfully handled initialization request');
365
- return toFetchResponse(res);
366
- };
367
- /**
368
- * Handles requests for existing MCP sessions
369
- */
370
- const handleExistingSessionRequest = async (body, executionContext, validatedContext, req, res, credentialStores) => {
371
- const { tenantId, projectId, graphId } = executionContext;
372
- // Validate the session id
373
- const conversation = await validateSession(req, res, body, tenantId, projectId, graphId);
374
- if (!conversation) {
375
- return toFetchResponse(res);
376
- }
377
- const sessionId = conversation.id;
378
- // Update last activity
379
- await updateConversation(dbClient)({
380
- scopes: { tenantId, projectId },
381
- conversationId: sessionId,
382
- data: {
383
- // Just updating the timestamp by calling update
384
- },
385
- });
386
- // Recreate transport and server from stored session data
387
- const transport = new StreamableHTTPServerTransport({
388
- sessionIdGenerator: () => sessionId,
389
- });
390
- const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
391
- await server.connect(transport);
392
- // Spoof initialization to set the transport's _initialized flag
393
- await spoofTransportInitialization(transport, req, sessionId, conversation.metadata?.session_data?.mcpProtocolVersion);
394
- logger.info({ sessionId }, 'Server connected and transport initialized');
395
- // Add debugging before transport.handleRequest()
396
- logger.info({
397
- sessionId,
398
- bodyKeys: Object.keys(body || {}),
399
- bodyMethod: body?.method,
400
- bodyId: body?.id,
401
- requestHeaders: Object.fromEntries(Object.entries(req.headers || {}).filter(([k]) => k.startsWith('mcp-'))),
402
- }, 'About to handle MCP request with existing session');
403
- try {
404
- await transport.handleRequest(req, res, body);
405
- logger.info({ sessionId }, 'Successfully handled MCP request');
406
- }
407
- catch (transportError) {
408
- logger.error({
409
- sessionId,
410
- error: transportError,
411
- errorMessage: transportError instanceof Error ? transportError.message : 'Unknown error',
412
- }, 'Transport handleRequest failed');
413
- throw transportError; // Re-throw to be caught by outer catch
414
- }
415
- return toFetchResponse(res);
416
- };
417
- /**
418
- * Creates a JSON-RPC error response
419
- */
420
- const createErrorResponse = (code, message, id = null) => ({
421
- jsonrpc: '2.0',
422
- error: { code, message },
423
- id,
424
- });
425
- app.openapi(createRoute({
426
- method: 'post',
427
- path: '/',
428
- tags: ['MCP'],
429
- summary: 'MCP Protocol',
430
- description: 'Handles Model Context Protocol (MCP) JSON-RPC requests',
431
- security: [{ bearerAuth: [] }],
432
- request: {
433
- headers: HeadersScopeSchema,
434
- },
435
- responses: {
436
- 200: {
437
- description: 'MCP response',
438
- },
439
- 401: {
440
- description: 'Unauthorized - API key authentication required',
441
- },
442
- 404: {
443
- description: 'Not Found - Agent graph not found',
444
- },
445
- 500: {
446
- description: 'Internal Server Error',
447
- },
448
- },
449
- }), async (c) => {
450
- try {
451
- // Validate parameters
452
- const paramValidation = validateRequestParameters(c);
453
- if (!paramValidation.valid) {
454
- return paramValidation.response;
455
- }
456
- const { executionContext } = paramValidation;
457
- const body = await c.req.json();
458
- logger.info({ body, bodyKeys: Object.keys(body || {}) }, 'Parsed request body');
459
- const isInitRequest = body.method === 'initialize';
460
- const { req, res } = toReqRes(c.req.raw);
461
- const validatedContext = c.get('validatedContext') || {};
462
- const credentialStores = c.get('credentialStores');
463
- logger.info({ validatedContext }, 'Validated context');
464
- logger.info({ req }, 'request');
465
- if (isInitRequest) {
466
- return await handleInitializationRequest(body, executionContext, validatedContext, req, res, c, credentialStores);
467
- }
468
- else {
469
- return await handleExistingSessionRequest(body, executionContext, validatedContext, req, res, credentialStores);
470
- }
471
- }
472
- catch (e) {
473
- logger.error({
474
- error: e instanceof Error ? e.message : e,
475
- stack: e instanceof Error ? e.stack : undefined,
476
- }, 'MCP request error');
477
- return c.json(createErrorResponse(-32603, 'Internal server error'), { status: 500 });
478
- }
479
- });
480
- app.get('/', async (c) => {
481
- logger.info('Received GET MCP request');
482
- return c.json({
483
- jsonrpc: '2.0',
484
- error: {
485
- code: -32000,
486
- message: 'Method not allowed.',
487
- },
488
- id: null,
489
- }, { status: 405 });
490
- });
491
- // We want to maintain conversations in the database. (https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#session-management)
492
- app.delete('/', async (c) => {
493
- logger.info('Received DELETE MCP request');
494
- return c.json({
495
- jsonrpc: '2.0',
496
- error: { code: -32001, message: 'Method Not Allowed' },
497
- id: null,
498
- }, { status: 405 });
499
- });
500
- export default app;
package/dist/server.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import './instrumentation.js';
2
- import { AgentExecutionServer } from './AgentExecutionServer.js';
3
- declare const executionServer: AgentExecutionServer;
4
- export { executionServer };
5
- //# sourceMappingURL=server.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAM9B,OAAO,EAAE,oBAAoB,EAAsB,MAAM,2BAA2B,CAAC;AAsBrF,QAAA,MAAM,eAAe,sBAQnB,CAAC;AAmCH,OAAO,EAAE,eAAe,EAAE,CAAC"}
package/dist/server.js DELETED
@@ -1,61 +0,0 @@
1
- import './instrumentation.js';
2
- import { createKeyChainStore, createNangoCredentialStore, InMemoryCredentialStore, } from '@inkeep/agents-core';
3
- import { AgentExecutionServer, EXECUTION_API_PORT } from './AgentExecutionServer.js';
4
- import { env } from './env.js';
5
- import { getLogger } from './logger.js';
6
- const logger = getLogger('execution-api');
7
- // Create credential stores
8
- const credentialStores = [
9
- new InMemoryCredentialStore('memory-default'), // In-memory store + env vars
10
- // Nango store (only loads if NANGO_SECRET_KEY is set)
11
- ...(process.env.NANGO_SECRET_KEY
12
- ? [
13
- createNangoCredentialStore('nango-default', {
14
- apiUrl: process.env.NANGO_HOST || 'https://api.nango.dev',
15
- secretKey: process.env.NANGO_SECRET_KEY,
16
- }),
17
- ]
18
- : []),
19
- createKeyChainStore('keychain-default'),
20
- ];
21
- // Initialize Execution Server
22
- const executionServer = new AgentExecutionServer({
23
- port: EXECUTION_API_PORT,
24
- credentialStores,
25
- serverOptions: {
26
- requestTimeout: 120000, // 120 seconds for execution requests
27
- keepAliveTimeout: 60000,
28
- keepAlive: true,
29
- },
30
- });
31
- // Start the server only if not in test environment AND not using Vite dev server
32
- const isViteDevServer = process.env.NODE_ENV === 'development' &&
33
- (process.env.VITE_DEV_SERVER === 'true' ||
34
- process.env.VITE !== undefined ||
35
- process.argv.some((arg) => arg.includes('vite')) ||
36
- globalThis.__vite_dev_server__);
37
- if (env.ENVIRONMENT !== 'test' && !isViteDevServer) {
38
- executionServer
39
- .serve()
40
- .then(() => {
41
- logger.info(`📝 OpenAPI documentation available at http://localhost:${EXECUTION_API_PORT}/openapi.json`);
42
- })
43
- .catch((error) => {
44
- logger.error('Failed to start Execution API server:', error);
45
- process.exit(1);
46
- });
47
- }
48
- else if (isViteDevServer) {
49
- // Initialize server (credential stores, etc.) but don't start HTTP server - Vite handles that
50
- (async () => {
51
- try {
52
- await executionServer.initializeOnly();
53
- logger.info('🚀 Execution server initialized for Vite dev mode (credential stores ready)');
54
- logger.info('🔥 HTTP server handled by Vite dev server');
55
- }
56
- catch (error) {
57
- logger.error('Failed to initialize execution server:', error);
58
- }
59
- })();
60
- }
61
- export { executionServer };
package/dist/tracer.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import { type Span, type Tracer } from '@opentelemetry/api';
2
- export declare const BASE = "inkeep-chat";
3
- export declare const SERVICE_NAME = "inkeep-chat";
4
- export declare const SERVICE_VERSION = "1.0.0";
5
- export declare const createSpanName: (suffix: string) => string;
6
- /**
7
- * Helper function to handle span errors consistently
8
- * Records the exception, sets error status, and optionally logs
9
- */
10
- export declare function handleSpanError(span: Span, error: unknown, logger?: {
11
- error: (obj: any, msg?: string) => void;
12
- }, logMessage?: string): void;
13
- /**
14
- * Get the global tracer instance
15
- * This creates a single tracer for the entire application
16
- */
17
- export declare function getGlobalTracer(): Tracer;
18
- /**
19
- * Force flush the tracer provider to ensure critical spans are sent immediately
20
- * This is useful for critical operations where we want to ensure telemetry data
21
- * is sent before the operation completes or fails
22
- */
23
- export declare function forceFlushTracer(): Promise<void>;
24
- //# sourceMappingURL=tracer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tracer.d.ts","sourceRoot":"","sources":["../src/tracer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,IAAI,EAGT,KAAK,MAAM,EAEZ,MAAM,oBAAoB,CAAC;AAU5B,eAAO,MAAM,IAAI,gBAAgB,CAAC;AAGlC,eAAO,MAAM,YAAY,gBAAgB,CAAC;AAC1C,eAAO,MAAM,eAAe,UAAU,CAAC;AAGvC,eAAO,MAAM,cAAc,GAAI,QAAQ,MAAM,WAAwB,CAAC;AAyCtE;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE;IAAE,KAAK,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CAAE,EACpD,UAAU,CAAC,EAAE,MAAM,GAClB,IAAI,CAgBN;AAED;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAUxC;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAwBtD"}