feat: Initial release MCP Outline PostgreSQL v1.0.0
86 tools across 12 modules for direct PostgreSQL access to Outline Wiki: - Documents (19), Collections (14), Users (9), Groups (8) - Comments (6), Shares (5), Revisions (3), Events (3) - Attachments (5), File Operations (4), OAuth (8), Auth (2) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
199
src/index.ts
Normal file
199
src/index.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* MCP Outline PostgreSQL - Main Server
|
||||
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
||||
*/
|
||||
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
import {
|
||||
ListToolsRequestSchema,
|
||||
CallToolRequestSchema,
|
||||
ListResourcesRequestSchema,
|
||||
ListPromptsRequestSchema
|
||||
} from '@modelcontextprotocol/sdk/types.js';
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
import { PgClient } from './pg-client.js';
|
||||
import { getDatabaseConfig } from './config/database.js';
|
||||
import { logger } from './utils/logger.js';
|
||||
import { checkRateLimit } from './utils/security.js';
|
||||
import { BaseTool } from './types/tools.js';
|
||||
|
||||
// Import ALL tools
|
||||
import {
|
||||
documentsTools,
|
||||
collectionsTools,
|
||||
usersTools,
|
||||
groupsTools,
|
||||
commentsTools,
|
||||
sharesTools,
|
||||
revisionsTools,
|
||||
eventsTools,
|
||||
attachmentsTools,
|
||||
fileOperationsTools,
|
||||
oauthTools,
|
||||
authTools
|
||||
} from './tools/index.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// Combine ALL tools into single array
|
||||
const allTools: BaseTool[] = [
|
||||
// Core functionality
|
||||
...documentsTools,
|
||||
...collectionsTools,
|
||||
...usersTools,
|
||||
...groupsTools,
|
||||
|
||||
// Collaboration
|
||||
...commentsTools,
|
||||
...sharesTools,
|
||||
...revisionsTools,
|
||||
|
||||
// System
|
||||
...eventsTools,
|
||||
...attachmentsTools,
|
||||
...fileOperationsTools,
|
||||
|
||||
// Authentication
|
||||
...oauthTools,
|
||||
...authTools
|
||||
];
|
||||
|
||||
// Validate all tools have required properties
|
||||
const invalidTools = allTools.filter((tool) => !tool.name || !tool.handler);
|
||||
if (invalidTools.length > 0) {
|
||||
logger.error(`${invalidTools.length} invalid tools found`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Get database configuration
|
||||
const config = getDatabaseConfig();
|
||||
|
||||
// Initialize PostgreSQL client
|
||||
const pgClient = new PgClient(config);
|
||||
|
||||
// Test database connection
|
||||
const isConnected = await pgClient.testConnection();
|
||||
if (!isConnected) {
|
||||
throw new Error('Failed to connect to PostgreSQL database');
|
||||
}
|
||||
|
||||
// Initialize MCP server
|
||||
const server = new Server({
|
||||
name: 'mcp-outline',
|
||||
version: '1.0.0'
|
||||
});
|
||||
|
||||
// Set capabilities (required for MCP v2.2+)
|
||||
(server as any)._capabilities = {
|
||||
tools: {},
|
||||
resources: {},
|
||||
prompts: {}
|
||||
};
|
||||
|
||||
// Connect transport BEFORE registering handlers
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
|
||||
// Register tools list handler
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
||||
tools: allTools.map((tool) => ({
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
inputSchema: tool.inputSchema
|
||||
}))
|
||||
}));
|
||||
|
||||
// Register resources handler (required even if empty)
|
||||
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
||||
logger.debug('Resources list requested');
|
||||
return { resources: [] };
|
||||
});
|
||||
|
||||
// Register prompts handler (required even if empty)
|
||||
server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
||||
logger.debug('Prompts list requested');
|
||||
return { prompts: [] };
|
||||
});
|
||||
|
||||
// Register tool call handler
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
const { name, arguments: args } = request.params;
|
||||
|
||||
// Rate limiting (using 'default' as clientId for now)
|
||||
const clientId = process.env.CLIENT_ID || 'default';
|
||||
if (!checkRateLimit('api', clientId)) {
|
||||
return {
|
||||
content: [
|
||||
{ type: 'text', text: 'Too Many Requests: rate limit exceeded. Try again later.' }
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
// Find the tool handler
|
||||
const tool = allTools.find((t) => t.name === name);
|
||||
|
||||
if (!tool) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Tool '${name}' not found`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// Pass the pool directly to tool handlers
|
||||
return await tool.handler(args as Record<string, unknown>, pgClient.getPool());
|
||||
} catch (error) {
|
||||
logger.error(`Error in tool ${name}:`, {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Error in tool ${name}: ${error instanceof Error ? error.message : String(error)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Log startup (minimal logging for MCP protocol compatibility)
|
||||
if (process.env.LOG_LEVEL !== 'error' && process.env.LOG_LEVEL !== 'none') {
|
||||
logger.info('MCP Server started');
|
||||
}
|
||||
|
||||
// Debug logging
|
||||
logger.debug('MCP Outline PostgreSQL Server running', {
|
||||
totalTools: allTools.length,
|
||||
toolsByModule: {
|
||||
documents: documentsTools.length,
|
||||
collections: collectionsTools.length,
|
||||
users: usersTools.length,
|
||||
groups: groupsTools.length,
|
||||
comments: commentsTools.length,
|
||||
shares: sharesTools.length,
|
||||
revisions: revisionsTools.length,
|
||||
events: eventsTools.length,
|
||||
attachments: attachmentsTools.length,
|
||||
fileOperations: fileOperationsTools.length,
|
||||
oauth: oauthTools.length,
|
||||
auth: authTools.length
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
logger.error('Fatal error', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined
|
||||
});
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user