Skip to content

Commit 54c98f1

Browse files
updated mcp server
1 parent 4bee06b commit 54c98f1

6 files changed

Lines changed: 611 additions & 47 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/dgraph-bulk-loader
55
/osx-docker-gopath
66

7+
/mcp/dgraph-mcp/dgraph-mcp
8+
79
# goland project folder
810
.idea
911
dgraph.iml

dgraph/cmd/alpha/run.go

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"time"
2626

2727
"github.com/golang/glog"
28-
"github.com/mark3labs/mcp-go/mcp"
2928
"github.com/mark3labs/mcp-go/server"
3029
"github.com/pkg/errors"
3130
"github.com/spf13/cobra"
@@ -46,6 +45,7 @@ import (
4645
"github.com/hypermodeinc/dgraph/v25/audit"
4746
"github.com/hypermodeinc/dgraph/v25/edgraph"
4847
"github.com/hypermodeinc/dgraph/v25/graphql/admin"
48+
dgraphmcp "github.com/hypermodeinc/dgraph/v25/mcp"
4949
"github.com/hypermodeinc/dgraph/v25/posting"
5050
"github.com/hypermodeinc/dgraph/v25/schema"
5151
"github.com/hypermodeinc/dgraph/v25/tok"
@@ -474,57 +474,23 @@ func serveGRPC(l net.Listener, tlsCfg *tls.Config, closer *z.Closer) {
474474
s.Stop()
475475
}
476476

477-
func setupMcp(baseMux *http.ServeMux) {
478-
s := server.NewMCPServer(
479-
"Dgraph MCP Server",
480-
"1.0.0",
481-
server.WithResourceCapabilities(true, true),
482-
server.WithLogging(),
483-
server.WithRecovery(),
484-
)
485-
486-
schemaTool := mcp.NewTool("Get-Schema",
487-
mcp.WithDescription("Get schema from dgraph db"),
488-
)
489-
490-
queryTool := mcp.NewTool("Run-DQL",
491-
mcp.WithDescription("Run DQL mutation / query on dgraph db"),
492-
mcp.WithString("query",
493-
mcp.Required(),
494-
mcp.Description("The query to perform. For mutations the format should look like {set {}}"),
495-
),
496-
)
497-
498-
s.AddTool(queryTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
499-
op := request.Params.Arguments["query"].(string)
500-
req := &apiv25.RunDQLRequest{
501-
NsName: "root",
502-
DqlQuery: op,
503-
}
504-
resp, err := (&edgraph.ServerV25{}).RunDQL(ctx, req)
505-
if err != nil {
506-
return mcp.NewToolResultError(err.Error()), nil
507-
}
508-
return mcp.NewToolResultText(string(resp.QueryResult)), nil
509-
})
510-
511-
s.AddTool(schemaTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
512-
req := &apiv25.RunDQLRequest{
513-
NsName: "root",
514-
DqlQuery: "schema {}",
515-
}
516-
resp, err := (&edgraph.ServerV25{}).RunDQL(ctx, req)
517-
if err != nil {
518-
return mcp.NewToolResultError(err.Error()), nil
519-
}
520-
return mcp.NewToolResultText(string(resp.QueryResult)), nil
521-
})
477+
func setupMcp(baseMux *http.ServeMux, connectionString, readOnly string) error {
478+
s, err := dgraphmcp.NewMCPServer(connectionString, readOnly)
479+
if err != nil {
480+
glog.Errorf("Failed to initialize MCPServer: %v\n", err)
481+
return err
482+
}
522483

523484
sse := server.NewSSEServer(s,
524485
server.WithBasePath("/mcp"),
525486
)
526487
baseMux.HandleFunc("/mcp", sse.ServeHTTP)
527488
baseMux.HandleFunc("/mcp/", sse.ServeHTTP)
489+
return nil
490+
}
491+
492+
func buildConnectionString(addr string, port int) string {
493+
return fmt.Sprintf("dgraph://%s:%d", addr, port)
528494
}
529495

530496
func setupServer(closer *z.Closer) {
@@ -563,7 +529,6 @@ func setupServer(closer *z.Closer) {
563529
baseMux.HandleFunc("/state", stateHandler)
564530
baseMux.HandleFunc("/debug/jemalloc", x.JemallocHandler)
565531
http.DefaultServeMux.Handle("/debug/z", zpages.NewTracezHandler(zpages.NewSpanProcessor()))
566-
setupMcp(baseMux)
567532

568533
// TODO: Figure out what this is for?
569534
http.HandleFunc("/debug/store", storeStatsHandler)
@@ -631,6 +596,10 @@ func setupServer(closer *z.Closer) {
631596
// Initialize the servers.
632597
x.ServerCloser.AddRunning(3)
633598
go serveGRPC(grpcListener, tlsCfg, x.ServerCloser)
599+
600+
if err := setupMcp(baseMux, buildConnectionString(laddr, grpcPort()), "true"); err != nil {
601+
log.Fatal(err)
602+
}
634603
go x.StartListenHttpAndHttps(httpListener, tlsCfg, x.ServerCloser)
635604

636605
go func() {

mcp/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Dgraph MCP (Master Control Protocol)
2+
3+
## Project Overview
4+
5+
This is a Dgraph-based Master Control Protocol (MCP) designed to provide advanced graph database
6+
management and querying capabilities. There are two ways you can access the mcp server:
7+
8+
1. Using the dgraph alpha's http endpoint
9+
2. Using the go code
10+
11+
## Key Components
12+
13+
- Dgraph schema management
14+
- Advanced data querying
15+
- Flexible graph database interactions
16+
17+
## Setup Instructions for dgraph alpha
18+
19+
- Install dgraph alpha
20+
- Start dgraph alpha
21+
22+
```json
23+
{
24+
"dgraph-mcp": {
25+
"serverUrl": "http://localhost:8080/mcp/sse"
26+
}
27+
}
28+
```
29+
30+
## Setup Instructions for go code
31+
32+
- Install golang 1.23+
33+
- Clone the repository
34+
- cd mcp/dgraph-mcp
35+
- go build
36+
- Insert the following to your mcp config:
37+
38+
```json
39+
{
40+
"mcpServers": {
41+
"dgraph": {
42+
"command": "<path to dgraph folder>/mcp/dgraph-mcp/dgraph-mcp",
43+
"env": {
44+
"DGRAPH_CONNECTION": "dgraph://<host>:443?sslmode=verify-ca&bearertoken=xxxxxxxxxx",
45+
"DGRAPH_READ_ONLY": "true" / "false",
46+
}
47+
}
48+
}
49+
}
50+
```
51+
52+
### Local Setup for Claude Desktop
53+
54+
You can setup a local experience using Claude app
55+
56+
Install Claude.app from https://claude.ai/download.
57+
58+
On Mac OS the configurations files are in `~/Library/Application\ Support/Claude`. Create
59+
`~/Library/Application\ Support/Claude/claude_desktop_config.json` file and set the content with mcp
60+
config shown above.
61+
62+
Close the Claude app and reopen it.
63+
64+
Check that the tools are listed in Claude desktop (Click on the tools icon).
65+
66+
You are ready to chat and get things done in Dgraph from Claude.
67+
68+
## Troubleshooting
69+
70+
- Check Dgraph connection settings
71+
- Verify Go module dependencies
72+
- Review error logs carefully

mcp/dgraph-mcp/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/mark3labs/mcp-go/server"
8+
9+
"github.com/hypermodeinc/dgraph/v25/mcp"
10+
)
11+
12+
func main() {
13+
// Get connection string from env
14+
connectionString := os.Getenv("DGRAPH_CONNECTION")
15+
readOnly := os.Getenv("DGRAPH_READ_ONLY")
16+
17+
s, err := mcp.NewMCPServer(connectionString, readOnly)
18+
if err != nil {
19+
fmt.Printf("Failed to initialize MCPServer: %v\n", err)
20+
return
21+
}
22+
23+
// Start the stdio server
24+
if err := server.ServeStdio(s); err != nil {
25+
fmt.Printf("Server error: %v\n", err)
26+
}
27+
}

0 commit comments

Comments
 (0)