import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';

const server = new Server(
  {
    name: 'my-project-mcp',
    version: '1.0.0',
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

// List available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: 'get_project_guidelines',
        description: 'Fetch coding standards and best practices',
        inputSchema: {
          type: 'object',
          properties: {
            category: {
              type: 'string',
              description: 'Guidelines category (testing, linting, etc.)',
            },
          },
        },
      },
      {
        name: 'run_project_tests',
        description: 'Execute test suite with custom arguments',
        inputSchema: {
          type: 'object',
          properties: {
            testPath: {
              type: 'string',
              description: 'Path to specific test file or directory',
            },
            args: {
              type: 'array',
              items: { type: 'string' },
              description: 'Additional test runner arguments',
            },
          },
        },
      },
    ],
  };
});

// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  const { name, arguments: args } = request.params;

  switch (name) {
    case 'get_project_guidelines':
      return await getProjectGuidelines(args.category);
    case 'run_project_tests':
      return await runProjectTests(args.testPath, args.args);
    default:
      throw new Error(`Unknown tool: ${name}`);
  }
});

// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);