import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { driftClient } from '../services/drift-client.js';
export function registerRouteTools(server: McpServer): void {
server.registerTool(
'driftos_route_message',
{
title: 'Route Message',
description: `Route a message to the appropriate conversation branch using semantic drift detection.
Returns one of three actions:
- BRANCH: New topic detected, creates a new branch
- STAY: Message continues the current topic
- ROUTE: Returns to a previously discussed topic
Args:
- conversation_id (string): Unique identifier for the conversation
- content (string): The message content to route
- role ('user' | 'assistant'): Who sent the message (default: 'user')
Returns:
{
"action": "BRANCH" | "STAY" | "ROUTE",
"branchId": string,
"branchTopic": string,
"confidence": number,
"isNewBranch": boolean
}
Example:
- "I want to buy a house in London" -> BRANCH (new topic)
- "What areas have good schools?" -> STAY (same topic)
- "Back to houses - what about mortgage rates?" -> ROUTE (returns to previous branch)`,
inputSchema: z.object({
conversation_id: z.string().min(1).describe('Unique identifier for the conversation'),
content: z.string().min(1).describe('The message content to route'),
role: z.enum(['user', 'assistant']).default('user').describe('Who sent the message'),
}).strict(),
annotations: {
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: false,
},
},
async (params) => {
try {
const result = await driftClient.route(
params.conversation_id,
params.content,
params.role
);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
return {
content: [
{
type: 'text' as const,
text: `Error routing message: ${message}`,
},
],
isError: true,
};
}
}
);
}