|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "mokapi/runtime/events" |
| 6 | + |
| 7 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 8 | +) |
| 9 | + |
| 10 | +type GetEventsInput struct { |
| 11 | + APIName string `json:"apiName"` |
| 12 | + Type string `json:"type"` |
| 13 | + Limit *int `json:"limit"` |
| 14 | + Traits map[string]string `json:"traits"` |
| 15 | +} |
| 16 | + |
| 17 | +type GetEventsResponse struct { |
| 18 | + Events []events.Event `json:"events"` |
| 19 | +} |
| 20 | + |
| 21 | +func (s *Service) registerGetEvents(server *mcp.Server) { |
| 22 | + inputSchema := map[string]any{ |
| 23 | + "type": "object", |
| 24 | + "properties": map[string]any{ |
| 25 | + "apiName": map[string]any{ |
| 26 | + "type": "string", |
| 27 | + "description": "Filter events by API name", |
| 28 | + }, |
| 29 | + "type": map[string]any{ |
| 30 | + "type": "string", |
| 31 | + "description": "Filter by event type", |
| 32 | + "enum": []string{"http", "kafka"}, |
| 33 | + }, |
| 34 | + "traits": map[string]any{ |
| 35 | + "type": "object", |
| 36 | + "description": "Filter events by traits", |
| 37 | + "additionalProperties": map[string]interface{}{ |
| 38 | + "type": "string", |
| 39 | + }, |
| 40 | + }, |
| 41 | + "limit": map[string]any{ |
| 42 | + "type": "integer", |
| 43 | + "description": "Maximum number of events to return", |
| 44 | + "default": 10, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + outputSchema := map[string]any{ |
| 50 | + "type": "object", |
| 51 | + "properties": map[string]any{ |
| 52 | + "events": map[string]any{ |
| 53 | + "type": "array", |
| 54 | + "description": "List of events", |
| 55 | + "items": map[string]any{ |
| 56 | + "type": "object", |
| 57 | + "properties": map[string]any{ |
| 58 | + "id": map[string]any{ |
| 59 | + "type": "string", |
| 60 | + "description": "ID of the event", |
| 61 | + }, |
| 62 | + "traits": map[string]any{ |
| 63 | + "type": "object", |
| 64 | + "description": "List of traits", |
| 65 | + "additionalProperties": map[string]interface{}{ |
| 66 | + "type": "string", |
| 67 | + }, |
| 68 | + }, |
| 69 | + "data": map[string]any{ |
| 70 | + "type": "object", |
| 71 | + "description": "The data of the event", |
| 72 | + }, |
| 73 | + "time": map[string]any{ |
| 74 | + "type": "string", |
| 75 | + "description": "Time of the event", |
| 76 | + "format": "date-time", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + registerTool(server, &mcp.Tool{ |
| 85 | + Name: "get_events", |
| 86 | + Description: `Returns recorded events from Mokapi including HTTP requests/responses and Kafka messages. |
| 87 | +
|
| 88 | +Use this tool when the user asks: |
| 89 | +- "What requests were made?" |
| 90 | +- "Why did my request fail?" |
| 91 | +- "Show recent API activity" |
| 92 | +- "What messages were produced to Kafka?" |
| 93 | +
|
| 94 | +Each event contains: |
| 95 | +- metadata: id, time, traits |
| 96 | +- HTTP data: request (method, URL, parameters, body) and response (status, headers, body, duration) |
| 97 | +- Kafka data: message payload, key, headers, partition, offset |
| 98 | +
|
| 99 | +Call this tool after sending requests or producing messages to inspect results and debug behavior.`, |
| 100 | + InputSchema: inputSchema, |
| 101 | + OutputSchema: outputSchema, |
| 102 | + }, s.ProduceKafkaMessage) |
| 103 | +} |
| 104 | + |
| 105 | +func (s *Service) GetEvents(_ context.Context, in GetEventsInput) (GetEventsResponse, error) { |
| 106 | + result := GetEventsResponse{} |
| 107 | + |
| 108 | + traits, err := bindInput[events.Traits](in.Traits) |
| 109 | + if err != nil { |
| 110 | + return result, err |
| 111 | + } |
| 112 | + if traits == nil { |
| 113 | + traits = events.NewTraits() |
| 114 | + } |
| 115 | + if in.Type != "" { |
| 116 | + traits.WithNamespace(in.Type) |
| 117 | + } |
| 118 | + |
| 119 | + evts := s.app.Events.GetEvents(traits) |
| 120 | + |
| 121 | + limit := 10 |
| 122 | + if in.Limit != nil { |
| 123 | + limit = *in.Limit |
| 124 | + } |
| 125 | + if len(evts) > limit { |
| 126 | + result.Events = evts[0:limit] |
| 127 | + } else { |
| 128 | + result.Events = evts |
| 129 | + } |
| 130 | + return result, nil |
| 131 | +} |
0 commit comments