-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseable_query.go
More file actions
49 lines (43 loc) · 2.01 KB
/
Copy pathparseable_query.go
File metadata and controls
49 lines (43 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package tools
import (
"context"
"encoding/json"
"fmt"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func RegisterQueryDataStreamTool(mcpServer *server.MCPServer) {
mcpServer.AddTool(mcp.NewTool(
"query_data_stream",
mcp.WithDescription("Execute a SQL query against a data stream in Parseable. All fields are required. All times must be in ISO 8601 format."),
mcp.WithString("query", mcp.Required(), mcp.Description("SQL query to run, but the FROM must always be set to streamName")),
mcp.WithString("streamName", mcp.Required(), mcp.Description("Name of the data stream (table)")),
mcp.WithString("startTime", mcp.Required(), mcp.Description("Query start time in ISO 8601 (format: yyyy-MM-ddTHH:mm:ss+hh:mm)")),
mcp.WithString("endTime", mcp.Required(), mcp.Description("Query end time in ISO 8601 (format: yyyy-MM-ddTHH:mm:ss+hh:mm)")),
), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
query := mcp.ParseString(req, "query", "")
streamName := mcp.ParseString(req, "streamName", "")
startTime := mcp.ParseString(req, "startTime", "")
endTime := mcp.ParseString(req, "endTime", "")
if query == "" || streamName == "" || startTime == "" || endTime == "" {
return mcp.NewToolResultError("missing required fields: query, streamName, startTime, and endTime are required"), nil
}
queryResult, err := doParseableQuery(query, streamName, startTime, endTime)
if err != nil {
return mcp.NewToolResultError("query failed: " + err.Error()), nil
}
b, err := json.MarshalIndent(queryResult, "", " ")
if err != nil {
return nil, err
}
// Optional: a one-liner summary that sets expectations.
text := fmt.Sprintf(
"Returned %d rows as JSON (array of objects). Use keys exactly as shown.\n```json\n%s\n```",
len(queryResult),
string(b),
)
return mcp.NewToolResultText(text), nil
// Optionally, for structured output:
// return mcp.NewToolResultStructured(map[string]interface{}{"result": result}, "Query successful"), nil
})
}