| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "os" |
| |
| "github.com/korap/korap-mcp/mcp" |
| mcplib "github.com/mark3labs/mcp-go/mcp" |
| ) |
| |
| // Run is the default command that starts the MCP server |
| func (c *CLI) Run() error { |
| // Handle version flag |
| if c.Version { |
| // For version output, we use stdout directly since logging isn't set up yet |
| fmt.Printf("%s version %s\n", c.GetServerName(), c.GetServerVersion()) |
| os.Exit(0) |
| } |
| |
| // Validate configuration and setup logging |
| logger, err := c.ValidateAndSetupLogging() |
| if err != nil { |
| return err |
| } |
| |
| logger.Info(). |
| Str("version", c.GetServerVersion()). |
| Str("korap_url", c.KorAP.BaseURL). |
| Bool("oauth_enabled", c.OAuth.Enabled). |
| Msg("KorAP MCP Server starting...") |
| |
| // Create MCP server |
| server := mcp.NewServer(c.GetServerName(), c.GetServerVersion()) |
| |
| // Add a simple ping tool for testing |
| err = server.AddTool( |
| "ping", |
| "Simple ping tool to test server connectivity", |
| map[string]any{ |
| "type": "object", |
| "properties": map[string]any{ |
| "message": map[string]any{ |
| "type": "string", |
| "description": "Message to echo back", |
| }, |
| }, |
| }, |
| func(ctx context.Context, request mcplib.CallToolRequest) (*mcplib.CallToolResult, error) { |
| message, err := request.RequireString("message") |
| if err != nil { |
| message = "pong" |
| } |
| logger.Debug(). |
| Str("message", message). |
| Msg("Ping tool called") |
| return mcplib.NewToolResultText(fmt.Sprintf("KorAP Server response: %s", message)), nil |
| }, |
| ) |
| |
| if err != nil { |
| return fmt.Errorf("failed to add ping tool: %w", err) |
| } |
| |
| logger.Info().Msg("Server ready - serving MCP via stdio") |
| |
| // Start the MCP server |
| if err := server.Serve(); err != nil { |
| return fmt.Errorf("server error: %w", err) |
| } |
| |
| return nil |
| } |
| |
| func main() { |
| // Setup CLI and parser |
| cli, parser, err := SetupCLI() |
| if err != nil { |
| // Before logging is set up, we use stderr directly for critical errors |
| fmt.Fprintf(os.Stderr, "Failed to setup CLI: %v\n", err) |
| os.Exit(1) |
| } |
| |
| // Parse command line arguments and run |
| ctx, err := parser.Parse(os.Args[1:]) |
| if err != nil { |
| parser.FatalIfErrorf(err) |
| } |
| |
| // Run the appropriate command (default or subcommand) |
| err = ctx.Run(cli) |
| ctx.FatalIfErrorf(err) |
| } |