#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
ErrorCode,
McpError,
} from '@modelcontextprotocol/sdk/types.js';
import { filePeek } from './tools.js';
import { getSupportedExtensions } from './parsers/index.js';
const server = new Server(
{
name: 'mcp-file-peek',
version: '0.2.0',
},
{
capabilities: {
tools: {},
},
}
);
const TOOLS = [
{
name: 'file_peek',
description: `Get a file's public interface (exported functions, classes, types, etc.) without reading the entire file. Uses AST parsing to extract only the public API surface.
Useful when you need to understand a file's API without consuming tokens on implementation details.
Supported languages: ${getSupportedExtensions().join(', ')}
For unsupported file types, returns an error — use the standard Read tool instead.`,
inputSchema: {
type: 'object' as const,
properties: {
path: {
type: 'string',
description: 'Path to the file (absolute or relative)',
},
},
required: ['path'],
},
},
];
server.setRequestHandler(ListToolsRequestSchema, async () => {
return { tools: TOOLS };
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case 'file_peek': {
const path = args?.path as string;
if (!path) {
throw new McpError(ErrorCode.InvalidParams, 'path is required');
}
const result = await filePeek(path);
return { content: [{ type: 'text', text: result }] };
}
default:
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
}
} catch (error) {
if (error instanceof McpError) {
throw error;
}
const message = error instanceof Error ? error.message : String(error);
return {
content: [{ type: 'text', text: `Error: ${message}` }],
isError: true,
};
}
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('MCP File Peek server running on stdio');
}
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});