import { SDK } from '@meetkai/mka1';
import type * as components from '@meetkai/mka1/models/components';
const mka1 = new SDK({
bearerAuth: `Bearer ${YOUR_API_KEY}`,
});
// Step 1: Create a background response with approval required
let pendingResponse = await mka1.llm.responses.create(
{
model: 'auto',
instructions:
'You are a project management assistant with access to Linear via MCP. Use Linear tools when the user asks about tasks, bugs, or projects. Keep the final answer terse.',
input: 'List my most recent Linear issue assigned to me.',
background: true,
store: true,
stream: false,
tools: [
{
type: 'mcp',
serverLabel: 'Linear MCP',
serverDescription: 'Access Linear issues through MCP.',
serverUrl: 'https://mcp.linear.app/mcp',
allowedTools: ['issues.list'],
headers: {
Authorization: `Bearer ${process.env.LINEAR_API_KEY}`,
},
requireApproval: 'always',
},
],
},
{ headers: { 'X-On-Behalf-Of': '<end-user-id>' } },
);
// Step 2: Poll until an approval request appears
let approvalRequest: components.MCPApprovalRequest | undefined;
while (
pendingResponse.status === 'queued' ||
pendingResponse.status === 'in_progress'
) {
approvalRequest = pendingResponse.output.find(
(item): item is components.MCPApprovalRequest =>
item.type === 'mcp_approval_request',
);
if (approvalRequest) break;
await new Promise(resolve => setTimeout(resolve, 1000));
pendingResponse = await mka1.llm.responses.get(
{ responseId: pendingResponse.id },
{ headers: { 'X-On-Behalf-Of': '<end-user-id>' } },
);
}
if (!approvalRequest) {
throw new Error(`No approval request found. Response ended with ${pendingResponse.status}.`);
}
// Step 3: Show the user what the model wants to do
console.log('Server:', approvalRequest.serverLabel);
console.log('Tool:', approvalRequest.name);
console.log('Arguments:', approvalRequest.arguments);
// Step 4: Send the approval (or denial) to continue
const approve = true; // Replace with your UI decision
const continuedResponse = await mka1.llm.responses.create(
{
model: 'auto',
previousResponseId: pendingResponse.id,
input: [
{
type: 'mcp_approval_response',
approvalRequestId: approvalRequest.id,
approve,
},
],
store: true,
stream: false,
},
{ headers: { 'X-On-Behalf-Of': '<end-user-id>' } },
);
const assistantText = continuedResponse.output
.filter(
(item): item is components.OutputMessage =>
item.type === 'message' && item.role === 'assistant',
)
.flatMap(item =>
item.content.flatMap(content =>
content.type === 'output_text' ? [content.text] : [],
),
)
.join('\n\n')
.trim();
console.log(assistantText);