@@ -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+
1619func 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