pagespace-mcp
Version:
Model Context Protocol (MCP) server for PageSpace with complete workspace management, powerful search, batch operations, and AI agent capabilities. Provides external access to all core PageSpace functionality.
300 lines (272 loc) • 9.54 kB
JavaScript
/**
* Agent handler for PageSpace MCP
* Implements agent creation, configuration, listing, and communication functionality
*/
export function createAgentHandler(api) {
return {
async handleCreateAgent(args) {
try {
const { driveId, parentId, title, systemPrompt, enabledTools, aiProvider, aiModel, welcomeMessage } = args;
console.error('[MCP] Creating AI agent:', { driveId, parentId, title, toolCount: enabledTools?.length || 0 });
const response = await api.request('POST', '/api/agents/create', {
driveId,
parentId,
title,
systemPrompt,
enabledTools,
aiProvider,
aiModel,
welcomeMessage
});
if (!response.success) {
throw new Error(response.error || 'Agent creation failed');
}
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
id: response.id,
title: response.title,
type: 'AI_CHAT',
parentId: parentId || 'root',
message: response.message || `Successfully created AI agent "${title}"`,
summary: response.summary || `Created AI agent with ${enabledTools?.length || 0} tools`,
agentConfig: response.agentConfig || {
systemPrompt: systemPrompt.substring(0, 100) + (systemPrompt.length > 100 ? '...' : ''),
enabledToolsCount: enabledTools?.length || 0,
enabledTools: enabledTools || [],
aiProvider: aiProvider || 'default',
aiModel: aiModel || 'default',
hasWelcomeMessage: !!welcomeMessage
},
stats: response.stats || {},
nextSteps: response.nextSteps || [
`AI agent "${title}" is ready to use`,
'Start a conversation to test the agent\'s behavior',
'Use read_page to view the agent\'s full configuration'
]
}, null, 2)
}
]
};
} catch (error) {
console.error('[MCP] Create agent error:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
error: `Failed to create AI agent: ${error.message}`,
driveId: args.driveId,
title: args.title
}, null, 2)
}
],
isError: true
};
}
},
async handleUpdateAgentConfig(args) {
try {
const { agentPath, agentId, systemPrompt, enabledTools, aiProvider, aiModel } = args;
console.error('[MCP] Updating agent config:', { agentPath, agentId });
const response = await api.request('PUT', `/api/agents/${agentId}/config`, {
systemPrompt,
enabledTools,
aiProvider,
aiModel
});
if (!response.success) {
throw new Error(response.error || 'Agent config update failed');
}
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
path: agentPath,
id: agentId,
title: response.title,
message: response.message || `Successfully updated AI agent configuration`,
summary: response.summary || `Updated agent configuration`,
updatedFields: response.updatedFields || [],
agentConfig: response.agentConfig || {},
nextSteps: response.nextSteps || [
'Test the agent to ensure the new configuration works as expected',
'The changes will take effect immediately in new conversations'
]
}, null, 2)
}
]
};
} catch (error) {
console.error('[MCP] Update agent config error:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
error: `Failed to update agent configuration: ${error.message}`,
agentPath: args.agentPath,
agentId: args.agentId
}, null, 2)
}
],
isError: true
};
}
},
async handleListAgents(args) {
try {
const { driveId, driveSlug, includeSystemPrompt = false, includeTools = true } = args;
console.error('[MCP] Listing agents in drive:', { driveId, driveSlug });
const params = new URLSearchParams({
includeSystemPrompt: includeSystemPrompt.toString(),
includeTools: includeTools.toString()
});
if (driveSlug) {
params.append('driveSlug', driveSlug);
}
const response = await api.request('GET', `/api/drives/${driveId}/agents?${params}`);
if (!response.success) {
throw new Error(response.error || 'Failed to list agents');
}
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
driveId: response.driveId,
driveName: response.driveName,
agents: response.agents || [],
count: response.count || 0,
summary: response.summary || `Found ${response.count || 0} agents`
}, null, 2)
}
]
};
} catch (error) {
console.error('[MCP] List agents error:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
error: `Failed to list agents: ${error.message}`,
driveId: args.driveId,
agents: [],
count: 0,
summary: 'Failed to list agents'
}, null, 2)
}
],
isError: true
};
}
},
async handleMultiDriveListAgents(args) {
try {
const { includeSystemPrompt = false, includeTools = true, groupByDrive = true } = args;
console.error('[MCP] Listing agents across all drives:', { includeSystemPrompt, includeTools, groupByDrive });
const params = new URLSearchParams({
includeSystemPrompt: includeSystemPrompt.toString(),
includeTools: includeTools.toString(),
groupByDrive: groupByDrive.toString()
});
const response = await api.request('GET', `/api/agents/multi-drive?${params}`);
if (!response.success) {
throw new Error(response.error || 'Failed to list agents across drives');
}
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
totalCount: response.totalCount || 0,
driveCount: response.driveCount || 0,
summary: response.summary || `Found ${response.totalCount || 0} agents`,
agentsByDrive: response.agentsByDrive || [],
agents: response.agents || []
}, null, 2)
}
]
};
} catch (error) {
console.error('[MCP] Multi-drive list agents error:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
error: `Failed to list agents across drives: ${error.message}`,
totalCount: 0,
driveCount: 0,
summary: 'Failed to list agents',
agentsByDrive: [],
agents: []
}, null, 2)
}
],
isError: true
};
}
},
async handleAskAgent(args) {
try {
const { agentPath, agentId, question, context } = args;
console.error('[MCP] Consulting agent:', { agentPath, agentId, question: question.substring(0, 50) });
const response = await api.request('POST', '/api/agents/consult', {
agentId,
question,
context
});
if (!response.success) {
throw new Error(response.error || 'Agent consultation failed');
}
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
agent: response.agent,
agentPath: agentPath,
question: question,
response: response.response,
context: context,
metadata: response.metadata || {}
}, null, 2)
}
]
};
} catch (error) {
console.error('[MCP] Ask agent error:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
agent: args.agentPath,
error: `Failed to consult agent: ${error.message}`,
question: args.question,
context: args.context,
metadata: {}
}, null, 2)
}
],
isError: true
};
}
}
};
}