|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + "github.com/neo4j/mcp/internal/config" |
| 9 | + "github.com/neo4j/mcp/internal/database" |
| 10 | + "github.com/neo4j/neo4j-go-driver/v5/neo4j" |
| 11 | +) |
| 12 | + |
| 13 | +func ReadCypherHandler(deps *ToolDependencies) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 14 | + return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 15 | + return handleReadCypher(ctx, request, deps.DBService, deps.Config) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func handleReadCypher(ctx context.Context, request mcp.CallToolRequest, dbService database.DatabaseService, config *config.Config) (*mcp.CallToolResult, error) { |
| 20 | + var args ReadCypherInput |
| 21 | + // Bind arguments to the struct |
| 22 | + if err := request.BindArguments(&args); err != nil { |
| 23 | + log.Printf("Error binding arguments: %v", err) |
| 24 | + return mcp.NewToolResultError(err.Error()), nil |
| 25 | + } |
| 26 | + Query := args.Query |
| 27 | + Params := args.Params |
| 28 | + |
| 29 | + log.Printf("cypher-query: %s", Query) |
| 30 | + |
| 31 | + // Validate that query is not empty |
| 32 | + if Query == "" { |
| 33 | + errMessage := "Query parameter is required and cannot be empty" |
| 34 | + log.Printf("%s", errMessage) |
| 35 | + return mcp.NewToolResultError(errMessage), nil |
| 36 | + } |
| 37 | + |
| 38 | + if dbService == nil { |
| 39 | + errMessage := "Database service is not initialized" |
| 40 | + log.Printf("%s", errMessage) |
| 41 | + return mcp.NewToolResultError(errMessage), nil |
| 42 | + } |
| 43 | + |
| 44 | + // Get queryType by pre-appending "EXPLAIN" to identify if the query is of type "r", if not raise a ToolResultError |
| 45 | + queryType, err := dbService.GetQueryType(ctx, Query, Params, config.Database) |
| 46 | + if err != nil { |
| 47 | + log.Printf("Error while classifying Cypher query: %v", err) |
| 48 | + return mcp.NewToolResultError(err.Error()), nil |
| 49 | + } |
| 50 | + |
| 51 | + if queryType != neo4j.StatementTypeReadOnly { // only queryType == "r" are allowed in read-cypher |
| 52 | + errMessage := "read-cypher can only run read-only Cypher statements. For write operations (CREATE, MERGE, DELETE, SET, etc...), schema/admin commands, or PROFILE queries, use write-cypher instead." |
| 53 | + log.Printf("Rejected non-read query (type=%v): %v", queryType, Query) |
| 54 | + return mcp.NewToolResultError(errMessage), nil |
| 55 | + } |
| 56 | + |
| 57 | + // Execute the Cypher query using the database service (now confirmed read-only) |
| 58 | + records, err := dbService.ExecuteReadQuery(ctx, Query, Params, config.Database) |
| 59 | + if err != nil { |
| 60 | + log.Printf("Error executing Cypher query: %v", err) |
| 61 | + return mcp.NewToolResultError(err.Error()), nil |
| 62 | + } |
| 63 | + |
| 64 | + response, err := dbService.Neo4jRecordsToJSON(records) |
| 65 | + if err != nil { |
| 66 | + log.Printf("Error formatting query results: %v", err) |
| 67 | + return mcp.NewToolResultError(err.Error()), nil |
| 68 | + } |
| 69 | + |
| 70 | + return mcp.NewToolResultText(response), nil |
| 71 | +} |
0 commit comments