@@ -13,16 +13,7 @@ import (
1313 "github.com/mark3labs/mcp-go/server"
1414)
1515
16- // NewMCPServer initializes and returns a new MCPServer instance.
17- func NewMCPServer (connectionString , readOnly string ) (* server.MCPServer , error ) {
18- s := server .NewMCPServer (
19- "Dgraph MCP Server" ,
20- "1.0.0" ,
21- server .WithResourceCapabilities (true , true ),
22- server .WithLogging (),
23- server .WithRecovery (),
24- )
25-
16+ func getConn (connectionString string ) (* dgo.Dgraph , error ) {
2617 conn , err := dgo .Open (connectionString )
2718 if err != nil {
2819 for i := range 10 {
@@ -36,9 +27,21 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
3627 return nil , fmt .Errorf ("error opening connection: %v" , err )
3728 }
3829 }
30+ return conn , nil
31+ }
32+
33+ // NewMCPServer initializes and returns a new MCPServer instance.
34+ func NewMCPServer (connectionString , readOnly string ) (* server.MCPServer , error ) {
35+ s := server .NewMCPServer (
36+ "Dgraph MCP Server" ,
37+ "1.0.0" ,
38+ server .WithResourceCapabilities (true , true ),
39+ server .WithLogging (),
40+ server .WithRecovery (),
41+ )
3942
4043 schemaTool := mcp .NewTool ("Get-Schema" ,
41- mcp .WithDescription ("Get schema from dgraph db" ),
44+ mcp .WithDescription ("Get Dgraph DQL Schema from dgraph db" ),
4245 mcp .WithToolAnnotation (mcp.ToolAnnotation {
4346 ReadOnlyHint : true ,
4447 DestructiveHint : false ,
@@ -47,22 +50,8 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
4750 }),
4851 )
4952
50- alterSchemaTool := mcp .NewTool ("Alter-Schema" ,
51- mcp .WithDescription ("Alter schema in dgraph db" ),
52- mcp .WithString ("schema" ,
53- mcp .Required (),
54- mcp .Description ("Updated schema to insert inside the db" ),
55- ),
56- mcp .WithToolAnnotation (mcp.ToolAnnotation {
57- ReadOnlyHint : false ,
58- DestructiveHint : true ,
59- IdempotentHint : false ,
60- OpenWorldHint : false ,
61- }),
62- )
63-
6453 queryTool := mcp .NewTool ("Run-Query" ,
65- mcp .WithDescription ("Run Query on dgraph db" ),
54+ mcp .WithDescription ("Run Dgraph Query on dgraph db" ),
6655 mcp .WithString ("query" ,
6756 mcp .Required (),
6857 mcp .Description ("The query to perform" ),
@@ -75,37 +64,60 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
7564 }),
7665 )
7766
78- mutationTool := mcp .NewTool ("Run-Mutation" ,
79- mcp .WithDescription ("Run Mutation on dgraph db" ),
80- mcp .WithString ("mutation" ,
81- mcp .Required (),
82- mcp .Description ("The mutation to perform in json format" ),
83- ),
84- mcp .WithToolAnnotation (mcp.ToolAnnotation {
85- ReadOnlyHint : false ,
86- DestructiveHint : true ,
87- IdempotentHint : false ,
88- OpenWorldHint : false ,
89- }),
90- )
91-
9267 if readOnly != "true" {
68+
69+ alterSchemaTool := mcp .NewTool ("Alter-Schema" ,
70+ mcp .WithDescription ("Alter Dgraph DQL Schema in dgraph db" ),
71+ mcp .WithString ("schema" ,
72+ mcp .Required (),
73+ mcp .Description ("Updated schema to insert inside the db" ),
74+ ),
75+ mcp .WithToolAnnotation (mcp.ToolAnnotation {
76+ ReadOnlyHint : false ,
77+ DestructiveHint : true ,
78+ IdempotentHint : false ,
79+ OpenWorldHint : false ,
80+ }),
81+ )
82+
9383 s .AddTool (alterSchemaTool , func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
9484 schema , ok := request .Params .Arguments ["schema" ].(string )
9585 if ! ok {
9686 return nil , fmt .Errorf ("schema must present" )
9787 }
9888
9989 // Execute alter operation
100- err := conn .SetSchema (ctx , "root" , schema )
90+ conn , err := getConn (connectionString )
91+ if err != nil {
92+ return nil , fmt .Errorf ("error opening connection: %v" , err )
93+ }
94+ err = conn .SetSchema (ctx , "root" , schema )
10195 if err != nil {
10296 return nil , fmt .Errorf ("schema alteration failed: %v" , err )
10397 }
10498
10599 return mcp .NewToolResultText ("Schema updated successfully" ), nil
106100 })
107101
102+ mutationTool := mcp .NewTool ("Run-Mutation" ,
103+ mcp .WithDescription ("Run DQL Mutation on dgraph db" ),
104+ mcp .WithString ("mutation" ,
105+ mcp .Required (),
106+ mcp .Description ("The mutation to perform in json format" ),
107+ ),
108+ mcp .WithToolAnnotation (mcp.ToolAnnotation {
109+ ReadOnlyHint : false ,
110+ DestructiveHint : true ,
111+ IdempotentHint : false ,
112+ OpenWorldHint : false ,
113+ }),
114+ )
115+
108116 s .AddTool (mutationTool , func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
117+ conn , err := getConn (connectionString )
118+ if err != nil {
119+ return nil , fmt .Errorf ("error opening connection: %v" , err )
120+ }
109121 txn := conn .NewTxn ()
110122 defer func () {
111123 err := txn .Discard (ctx )
@@ -130,6 +142,10 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
130142 }
131143
132144 s .AddTool (queryTool , func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
145+ conn , err := getConn (connectionString )
146+ if err != nil {
147+ return nil , fmt .Errorf ("error opening connection: %v" , err )
148+ }
133149 txn := conn .NewTxn ()
134150 defer func () {
135151 err := txn .Discard (ctx )
@@ -146,6 +162,10 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
146162 })
147163
148164 s .AddTool (schemaTool , func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
165+ conn , err := getConn (connectionString )
166+ if err != nil {
167+ return nil , fmt .Errorf ("error opening connection: %v" , err )
168+ }
149169 txn := conn .NewTxn ()
150170 defer func () {
151171 err := txn .Discard (ctx )
@@ -169,6 +189,10 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
169189
170190 s .AddResource (schemaResource , func (ctx context.Context , request mcp.ReadResourceRequest ) ([]mcp.ResourceContents , error ) {
171191 // Execute operation
192+ conn , err := getConn (connectionString )
193+ if err != nil {
194+ return nil , fmt .Errorf ("error opening connection: %v" , err )
195+ }
172196 resp , err := conn .NewTxn ().Query (ctx , "schema {}" )
173197 if err != nil {
174198 return nil , fmt .Errorf ("failed to get schema: %v" , err )
@@ -256,6 +280,10 @@ func NewMCPServer(connectionString, readOnly string) (*server.MCPServer, error)
256280 // Add resource with its handler
257281 s .AddResource (schemaResource , func (ctx context.Context , request mcp.ReadResourceRequest ) ([]mcp.ResourceContents , error ) {
258282 // Execute operation
283+ conn , err := getConn (connectionString )
284+ if err != nil {
285+ return nil , fmt .Errorf ("error opening connection: %v" , err )
286+ }
259287 resp , err := conn .NewTxn ().Query (ctx , "schema {}" )
260288 if err != nil {
261289 return nil , fmt .Errorf ("failed to get schema: %v" , err )
0 commit comments