blob: d908f1024d3e5511ba3a116cad99be912dfd75a9 [file] [log] [blame]
Akron90f65212025-06-12 14:32:55 +02001package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7
8 "github.com/korap/korap-mcp/mcp"
9 mcplib "github.com/mark3labs/mcp-go/mcp"
10)
11
12// Run is the default command that starts the MCP server
13func (c *CLI) Run() error {
14 // Handle version flag
15 if c.Version {
Akronfc281172025-06-12 15:14:05 +020016 // For version output, we use stdout directly since logging isn't set up yet
Akron90f65212025-06-12 14:32:55 +020017 fmt.Printf("%s version %s\n", c.GetServerName(), c.GetServerVersion())
18 os.Exit(0)
19 }
20
21 // Validate configuration and setup logging
22 logger, err := c.ValidateAndSetupLogging()
23 if err != nil {
24 return err
25 }
26
27 logger.Info().
28 Str("version", c.GetServerVersion()).
29 Str("korap_url", c.KorAP.BaseURL).
30 Bool("oauth_enabled", c.OAuth.Enabled).
31 Msg("KorAP MCP Server starting...")
32
33 // Create MCP server
34 server := mcp.NewServer(c.GetServerName(), c.GetServerVersion())
35
36 // Add a simple ping tool for testing
37 err = server.AddTool(
38 "ping",
39 "Simple ping tool to test server connectivity",
Akron708f3912025-06-17 12:26:02 +020040 map[string]any{
Akron90f65212025-06-12 14:32:55 +020041 "type": "object",
Akron708f3912025-06-17 12:26:02 +020042 "properties": map[string]any{
43 "message": map[string]any{
Akron90f65212025-06-12 14:32:55 +020044 "type": "string",
45 "description": "Message to echo back",
46 },
47 },
48 },
49 func(ctx context.Context, request mcplib.CallToolRequest) (*mcplib.CallToolResult, error) {
50 message, err := request.RequireString("message")
51 if err != nil {
52 message = "pong"
53 }
54 logger.Debug().
55 Str("message", message).
56 Msg("Ping tool called")
57 return mcplib.NewToolResultText(fmt.Sprintf("KorAP Server response: %s", message)), nil
58 },
59 )
60
61 if err != nil {
62 return fmt.Errorf("failed to add ping tool: %w", err)
63 }
64
65 logger.Info().Msg("Server ready - serving MCP via stdio")
66
67 // Start the MCP server
68 if err := server.Serve(); err != nil {
69 return fmt.Errorf("server error: %w", err)
70 }
71
72 return nil
73}
74
75func main() {
76 // Setup CLI and parser
77 cli, parser, err := SetupCLI()
78 if err != nil {
Akronfc281172025-06-12 15:14:05 +020079 // Before logging is set up, we use stderr directly for critical errors
Akron90f65212025-06-12 14:32:55 +020080 fmt.Fprintf(os.Stderr, "Failed to setup CLI: %v\n", err)
81 os.Exit(1)
82 }
83
84 // Parse command line arguments and run
85 ctx, err := parser.Parse(os.Args[1:])
86 if err != nil {
87 parser.FatalIfErrorf(err)
88 }
89
90 // Run the appropriate command (default or subcommand)
91 err = ctx.Run(cli)
92 ctx.FatalIfErrorf(err)
93}