Akron | b1c71e6 | 2025-06-12 16:08:54 +0200 | [diff] [blame^] | 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/korap/korap-mcp/service" |
| 8 | "github.com/mark3labs/mcp-go/mcp" |
| 9 | "github.com/rs/zerolog/log" |
| 10 | ) |
| 11 | |
| 12 | // SearchTool implements the Tool interface for KorAP corpus search |
| 13 | type SearchTool struct { |
| 14 | client *service.Client |
| 15 | } |
| 16 | |
| 17 | // NewSearchTool creates a new search tool instance |
| 18 | func NewSearchTool(client *service.Client) *SearchTool { |
| 19 | return &SearchTool{ |
| 20 | client: client, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // Name returns the tool name |
| 25 | func (s *SearchTool) Name() string { |
| 26 | return "korap_search" |
| 27 | } |
| 28 | |
| 29 | // Description returns the tool description |
| 30 | func (s *SearchTool) Description() string { |
| 31 | return "Search for words or phrases in KorAP corpora using various query languages" |
| 32 | } |
| 33 | |
| 34 | // InputSchema returns the JSON schema for tool parameters |
| 35 | func (s *SearchTool) InputSchema() map[string]interface{} { |
| 36 | return map[string]interface{}{ |
| 37 | "type": "object", |
| 38 | "properties": map[string]interface{}{ |
| 39 | "query": map[string]interface{}{ |
| 40 | "type": "string", |
| 41 | "description": "The search query (word, phrase, or pattern)", |
| 42 | }, |
| 43 | "query_language": map[string]interface{}{ |
| 44 | "type": "string", |
| 45 | "description": "Query language: 'poliqarp' (default), 'cosmas2', or 'annis'", |
| 46 | "enum": []string{"poliqarp", "cosmas2", "annis"}, |
| 47 | "default": "poliqarp", |
| 48 | }, |
| 49 | "corpus": map[string]interface{}{ |
| 50 | "type": "string", |
| 51 | "description": "Virtual corpus to search in", |
| 52 | }, |
| 53 | "count": map[string]interface{}{ |
| 54 | "type": "integer", |
| 55 | "description": "Number of results to return (max 100)", |
| 56 | "minimum": 1, |
| 57 | "maximum": 100, |
| 58 | "default": 25, |
| 59 | }, |
| 60 | }, |
| 61 | "required": []string{"query"}, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Execute performs the search operation |
| 66 | func (s *SearchTool) Execute(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 67 | log.Debug(). |
| 68 | Str("tool", s.Name()). |
| 69 | Msg("Executing search tool") |
| 70 | |
| 71 | // Extract required query parameter using the same pattern as main.go |
| 72 | query, err := request.RequireString("query") |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("query parameter is required: %w", err) |
| 75 | } |
| 76 | |
| 77 | log.Debug(). |
| 78 | Str("query", query). |
| 79 | Msg("Parsed search parameters") |
| 80 | |
| 81 | // For now, return a simple response to verify the tool works |
| 82 | // TODO: Implement actual KorAP search functionality |
| 83 | result := "KorAP Search Results\n" |
| 84 | result += "====================\n\n" |
| 85 | result += fmt.Sprintf("Query: %s\n", query) |
| 86 | result += "Status: Tool is working, KorAP integration to be implemented\n" |
| 87 | |
| 88 | log.Info(). |
| 89 | Str("query", query). |
| 90 | Msg("Search tool executed successfully") |
| 91 | |
| 92 | return mcp.NewToolResultText(result), nil |
| 93 | } |