UNPKG

plaier-mcp-server

Version:

MCP Server for Plaier Football API - Advanced football analytics and predictions

264 lines 10.8 kB
#!/usr/bin/env node /** * Plaier MCP Server - Main entry point * * This server provides access to the Plaier Football API through the Model Context Protocol. * It offers comprehensive football analytics including team data, player statistics, * match predictions, tournament analysis, and transfer impact assessments. */ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js'; // Import API client and configuration import { PlaierApiClient } from './api/client.js'; import { getConfig } from './config.js'; // Import tool creators and handlers import { createTeamTools, handleTeamTool } from './tools/team-tools.js'; import { createPlayerTools, handlePlayerTool } from './tools/player-tools.js'; import { createMatchTools, handleMatchTool } from './tools/match-tools.js'; import { createTournamentTools, handleTournamentTool } from './tools/tournament-tools.js'; import { createTransferTools, handleTransferTool } from './tools/transfer-tools.js'; import { createNationalityTools, handleNationalityTool } from './tools/nationality-tools.js'; import { createPositionTools, handlePositionTool } from './tools/position-tools.js'; // Import utilities import { formatError } from './utils/formatting.js'; import { fileURLToPath } from 'url'; import { dirname, resolve } from 'path'; import { readFileSync } from 'fs'; // Get package.json version const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const packageJson = JSON.parse(readFileSync(resolve(__dirname, "../package.json"), "utf8")); const VERSION = packageJson.version; /** * Main server class */ class PlaierMcpServer { server; client; serverInfo = { name: 'plaier-mcp-server', version: VERSION, }; constructor() { this.server = new Server(this.serverInfo, { capabilities: { tools: {}, }, }); // Initialize API client try { const config = getConfig(); this.client = new PlaierApiClient(config); } catch (error) { console.error('Failed to initialize Plaier API client:', error); process.exit(1); } this.setupToolHandlers(); this.setupErrorHandling(); } /** * Set up tool handlers */ setupToolHandlers() { // Create all tools const allTools = [ ...createTeamTools(this.client), ...createPlayerTools(this.client), ...createMatchTools(this.client), ...createTournamentTools(this.client), ...createTransferTools(this.client), ...createNationalityTools(this.client), ...createPositionTools(this.client), ]; // Register list_tools handler this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: allTools, }; }); // Register call_tool handler this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { // Route to appropriate tool handler based on tool name if ([ "search_teams", "get_team_details", "get_teams_by_tournament", "get_team_performance_in_tournament", "get_team_improvement", "get_team_most_important_players", "get_all_teams" ].includes(name)) { return { content: [ { type: 'text', text: await handleTeamTool(name, args, this.client), }, ], }; } if (name.startsWith('search_players') || name.startsWith('get_player') || name.startsWith('rank_players') || name.startsWith('get_team_players') || name.startsWith('get_tournament_players')) { return { content: [ { type: 'text', text: await handlePlayerTool(name, args, this.client), }, ], }; } if (name.startsWith('predict_match') || name.startsWith('get_best_lineups') || name.startsWith('get_best_formations') || name.startsWith('get_team_schedule') || name.startsWith('simulate_match') || name.startsWith('get_lineup_count')) { return { content: [ { type: 'text', text: await handleMatchTool(name, args, this.client), }, ], }; } if (name.startsWith('get_tournaments') || name.startsWith('search_tournaments') || name.startsWith('predict_tournament') || name.startsWith('get_tournament_fixtures') || name.startsWith('get_tournament_probabilities')) { return { content: [ { type: 'text', text: await handleTournamentTool(name, args, this.client), }, ], }; } if (name.startsWith('get_transfer_impact')) { return { content: [ { type: 'text', text: await handleTransferTool(name, args, this.client), }, ], }; } if (name.startsWith('get_nationalities')) { return { content: [ { type: 'text', text: await handleNationalityTool(name, args, this.client), }, ], }; } if (name.startsWith('get_position') || name === 'get_positions') { return { content: [ { type: 'text', text: await handlePositionTool(name, args, this.client), }, ], }; } // If no handler found throw new Error(`Unknown tool: ${name}`); } catch (error) { console.error(`Error executing tool ${name}:`, error); return { content: [ { type: 'text', text: formatError(`Tool execution failed: ${error}`), }, ], isError: true, }; } }); } /** * Set up error handling */ setupErrorHandling() { // Handle server errors this.server.onerror = (error) => { console.error('MCP Server error:', error); }; // Handle process errors process.on('SIGINT', async () => { console.error('\nShutting down Plaier MCP Server...'); await this.server.close(); process.exit(0); }); process.on('SIGTERM', async () => { console.error('\nShutting down Plaier MCP Server...'); await this.server.close(); process.exit(0); }); process.on('uncaughtException', (error) => { console.error('Uncaught exception:', error); process.exit(1); }); process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled rejection at:', promise, 'reason:', reason); process.exit(1); }); } /** * Start the server */ async start() { const transport = new StdioServerTransport(); console.error('Starting Plaier MCP Server...'); console.error('Server info:'); console.error(` Name: ${this.serverInfo.name}`); console.error(` Version: ${this.serverInfo.version}`); console.error(' API: Plaier Football API'); console.error(' Capabilities: Team analysis, Player rankings, Match predictions, Tournament analysis, Transfer impact'); console.error(''); console.error('Tools available (27 total):'); console.error(' 🏟️ Teams (7): search_teams, get_team_details, get_teams_by_tournament, get_team_performance_in_tournament, get_team_improvement, get_team_most_important_players, get_all_teams'); console.error(' ⚽ Players (5): search_players, get_player_details, rank_players, get_team_players, get_tournament_players'); console.error(' 🎯 Matches (6): predict_match, get_best_lineups, get_best_formations, get_lineup_count, get_team_schedule, simulate_match'); console.error(' 🏆 Tournaments (5): get_tournaments, search_tournaments, get_tournament_fixtures, predict_tournament, get_tournament_probabilities'); console.error(' 💰 Transfers (1): get_transfer_impact'); console.error(' 📍 Positions (2): get_positions, get_position_groups'); console.error(' 🌍 Nationalities (1): get_nationalities'); console.error(''); console.error('Ready to receive requests via stdio...'); await this.server.connect(transport); } } /** * Main execution */ async function main() { try { const server = new PlaierMcpServer(); await server.start(); } catch (error) { console.error('Failed to start Plaier MCP Server:', error); process.exit(1); } } main().catch((error) => { console.error('Server startup failed:', error); process.exit(1); }); //# sourceMappingURL=index.js.map