11import { Router } from 'express' ;
22import { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
33import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js' ;
4- import type { ProjectInstance } from '@/lib/project-manager' ;
5- import { createMcpServer } from '@/api/index' ;
4+ import type { ProjectInstance , ProjectManager } from '@/lib/project-manager' ;
5+ import { createMcpServer , type McpSessionContext } from '@/api/index' ;
66
77// Tool category detection based on tool name
88const TOOL_CATEGORIES : Record < string , string > = {
9+ get_context : 'context' ,
910 list_topics : 'docs' , get_toc : 'docs' , search : 'docs' , get_node : 'docs' ,
1011 search_topic_files : 'docs' , find_examples : 'docs' , search_snippets : 'docs' ,
1112 list_snippets : 'docs' , explain_symbol : 'docs' , cross_references : 'cross-graph' ,
@@ -16,23 +17,40 @@ const TOOL_CATEGORIES: Record<string, string> = {
1617 get_note : 'knowledge' , list_notes : 'knowledge' , search_notes : 'knowledge' ,
1718 create_relation : 'knowledge' , delete_relation : 'knowledge' ,
1819 list_relations : 'knowledge' , find_linked_notes : 'knowledge' ,
20+ add_note_attachment : 'knowledge' , remove_note_attachment : 'knowledge' ,
1921 create_task : 'tasks' , update_task : 'tasks' , delete_task : 'tasks' ,
2022 get_task : 'tasks' , list_tasks : 'tasks' , search_tasks : 'tasks' ,
2123 move_task : 'tasks' , link_task : 'tasks' , create_task_link : 'tasks' ,
2224 delete_task_link : 'tasks' , find_linked_tasks : 'tasks' ,
25+ add_task_attachment : 'tasks' , remove_task_attachment : 'tasks' ,
26+ create_skill : 'skills' , update_skill : 'skills' , delete_skill : 'skills' ,
27+ get_skill : 'skills' , list_skills : 'skills' , search_skills : 'skills' ,
28+ recall_skills : 'skills' , bump_skill_usage : 'skills' ,
29+ link_skill : 'skills' , create_skill_link : 'skills' , delete_skill_link : 'skills' ,
30+ find_linked_skills : 'skills' ,
31+ add_skill_attachment : 'skills' , remove_skill_attachment : 'skills' ,
2332} ;
2433
2534/**
2635 * Get or create a lazy MCP client for a project instance.
2736 * The client is cached on the instance for reuse.
2837 */
29- async function getClient ( p : ProjectInstance ) : Promise < Client > {
38+ async function getClient ( p : ProjectInstance , pm : ProjectManager ) : Promise < Client > {
3039 if ( p . mcpClient ) return p . mcpClient ;
3140
41+ // Build session context for get_context tool
42+ const ws = p . workspaceId ? pm . getWorkspace ( p . workspaceId ) : undefined ;
43+ const sessionCtx : McpSessionContext = {
44+ projectId : p . id ,
45+ workspaceId : ws ?. id ,
46+ workspaceProjects : ws ?. config . projects ,
47+ } ;
48+
3249 const [ serverTransport , clientTransport ] = InMemoryTransport . createLinkedPair ( ) ;
3350 const server = createMcpServer (
3451 p . docGraph , p . codeGraph , p . knowledgeGraph , p . fileIndexGraph ,
3552 p . taskGraph , p . embedFns , p . mutationQueue ,
53+ p . config . projectDir , p . skillGraph , sessionCtx ,
3654 ) ;
3755 await server . connect ( serverTransport ) ;
3856
@@ -49,7 +67,7 @@ async function getClient(p: ProjectInstance): Promise<Client> {
4967 return client ;
5068}
5169
52- export function createToolsRouter ( ) : Router {
70+ export function createToolsRouter ( projectManager : ProjectManager ) : Router {
5371 const router = Router ( { mergeParams : true } ) ;
5472
5573 function getProject ( req : any ) : ProjectInstance {
@@ -60,7 +78,7 @@ export function createToolsRouter(): Router {
6078 router . get ( '/' , async ( req , res , next ) => {
6179 try {
6280 const p = getProject ( req ) ;
63- const client = await getClient ( p ) ;
81+ const client = await getClient ( p , projectManager ) ;
6482 const { tools } = await client . listTools ( ) ;
6583 const results = tools . map ( t => ( {
6684 name : t . name ,
@@ -76,7 +94,7 @@ export function createToolsRouter(): Router {
7694 router . get ( '/:toolName' , async ( req , res , next ) => {
7795 try {
7896 const p = getProject ( req ) ;
79- const client = await getClient ( p ) ;
97+ const client = await getClient ( p , projectManager ) ;
8098 const { tools } = await client . listTools ( ) ;
8199 const tool = tools . find ( t => t . name === req . params . toolName ) ;
82100 if ( ! tool ) return res . status ( 404 ) . json ( { error : `Tool "${ req . params . toolName } " not found` } ) ;
@@ -93,7 +111,7 @@ export function createToolsRouter(): Router {
93111 router . post ( '/:toolName/call' , async ( req , res , next ) => {
94112 try {
95113 const p = getProject ( req ) ;
96- const client = await getClient ( p ) ;
114+ const client = await getClient ( p , projectManager ) ;
97115 const start = Date . now ( ) ;
98116 const result = await client . callTool ( {
99117 name : req . params . toolName ,
0 commit comments