Skip to content

Commit 11ec882

Browse files
fixed some more reviews
1 parent 488bc90 commit 11ec882

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

dgraph/cmd/alpha/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func serveGRPC(l net.Listener, tlsCfg *tls.Config, closer *z.Closer) {
479479
func setupMcp(baseMux *http.ServeMux, connectionString, readOnly string) error {
480480
s, err := dgraphmcp.NewMCPServer(connectionString, readOnly)
481481
if err != nil {
482-
glog.Errorf("Failed to initialize MCPServer: %v\n", err)
482+
glog.Errorf("Failed to initialize MCPServer: %v", err)
483483
return err
484484
}
485485

mcp/dgraph-mcp/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

6+
"github.com/golang/glog"
77
"github.com/mark3labs/mcp-go/server"
88

99
"github.com/hypermodeinc/dgraph/v25/mcp"
@@ -16,12 +16,12 @@ func main() {
1616

1717
s, err := mcp.NewMCPServer(connectionString, readOnly)
1818
if err != nil {
19-
fmt.Printf("Failed to initialize MCPServer: %v\n", err)
19+
glog.Errorf("Failed to initialize MCPServer: %v", err)
2020
return
2121
}
2222

2323
// Start the stdio server
2424
if err := server.ServeStdio(s); err != nil {
25-
fmt.Printf("Server error: %v\n", err)
25+
glog.Errorf("Server error: %v", err)
2626
}
2727
}

mcp/mcp_server.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ import (
88

99
"github.com/dgraph-io/dgo/v250"
1010
"github.com/dgraph-io/dgo/v250/protos/api"
11+
1112
"github.com/golang/glog"
1213
"github.com/mark3labs/mcp-go/mcp"
1314
"github.com/mark3labs/mcp-go/server"
1415
)
1516

17+
var dgraphConnection *dgo.Dgraph
18+
1619
func getConn(connectionString string) (*dgo.Dgraph, error) {
20+
if dgraphConnection != nil {
21+
return dgraphConnection, nil
22+
}
23+
1724
conn, err := dgo.Open(connectionString)
1825
if err != nil {
1926
for i := range 10 {
@@ -24,9 +31,10 @@ func getConn(connectionString string) (*dgo.Dgraph, error) {
2431
}
2532
}
2633
if err != nil {
27-
return nil, fmt.Errorf("error opening connection: %v", err)
34+
return nil, fmt.Errorf("error opening connection with Dgraph Alpha: %v", err)
2835
}
2936
}
37+
dgraphConnection = conn
3038
return conn, nil
3139
}
3240

@@ -65,7 +73,6 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
6573
)
6674

6775
if readOnly != "true" {
68-
6976
alterSchemaTool := mcp.NewTool("Alter-Schema",
7077
mcp.WithDescription("Alter Dgraph DQL Schema in dgraph db"),
7178
mcp.WithString("schema",
@@ -83,16 +90,15 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
8390
s.AddTool(alterSchemaTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
8491
schema, ok := request.Params.Arguments["schema"].(string)
8592
if !ok {
86-
return nil, fmt.Errorf("schema must present")
93+
return nil, fmt.Errorf("schema must be present")
8794
}
8895

8996
// Execute alter operation
9097
conn, err := getConn(connectionString)
9198
if err != nil {
92-
return nil, fmt.Errorf("error opening connection: %v", err)
99+
return nil, fmt.Errorf("error opening connection with Dgraph Alpha: %v", err)
93100
}
94-
err = conn.SetSchema(ctx, "root", schema)
95-
if err != nil {
101+
if err = conn.SetSchema(ctx, dgo.RootNamespace, schema); err != nil {
96102
return nil, fmt.Errorf("schema alteration failed: %v", err)
97103
}
98104

@@ -116,7 +122,7 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
116122
s.AddTool(mutationTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
117123
conn, err := getConn(connectionString)
118124
if err != nil {
119-
return nil, fmt.Errorf("error opening connection: %v", err)
125+
return nil, err
120126
}
121127
txn := conn.NewTxn()
122128
defer func() {
@@ -129,11 +135,10 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
129135
if !ok {
130136
return nil, fmt.Errorf("mutation must present")
131137
}
132-
resp, err := txn.Mutate(ctx, &api.Mutation{SetJson: []byte(mutation)})
133-
if err != nil {
134-
return mcp.NewToolResultError(err.Error()), nil
135-
}
136-
err = txn.Commit(ctx)
138+
resp, err := txn.Mutate(ctx, &api.Mutation{
139+
SetJson: []byte(mutation),
140+
CommitNow: true,
141+
})
137142
if err != nil {
138143
return mcp.NewToolResultError(err.Error()), nil
139144
}
@@ -144,7 +149,7 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
144149
s.AddTool(queryTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
145150
conn, err := getConn(connectionString)
146151
if err != nil {
147-
return nil, fmt.Errorf("error opening connection: %v", err)
152+
return nil, err
148153
}
149154
txn := conn.NewTxn()
150155
defer func() {
@@ -164,7 +169,7 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
164169
s.AddTool(schemaTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
165170
conn, err := getConn(connectionString)
166171
if err != nil {
167-
return nil, fmt.Errorf("error opening connection: %v", err)
172+
return nil, err
168173
}
169174
txn := conn.NewTxn()
170175
defer func() {
@@ -191,7 +196,7 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
191196
// Execute operation
192197
conn, err := getConn(connectionString)
193198
if err != nil {
194-
return nil, fmt.Errorf("error opening connection: %v", err)
199+
return nil, err
195200
}
196201
resp, err := conn.NewTxn().Query(ctx, "schema {}")
197202
if err != nil {
@@ -282,7 +287,7 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
282287
// Execute operation
283288
conn, err := getConn(connectionString)
284289
if err != nil {
285-
return nil, fmt.Errorf("error opening connection: %v", err)
290+
return nil, err
286291
}
287292
resp, err := conn.NewTxn().Query(ctx, "schema {}")
288293
if err != nil {

0 commit comments

Comments
 (0)