@@ -7,9 +7,10 @@ import {
77} from "@modelcontextprotocol/sdk/server/mcp.js" ;
88// @ts -expect-error dkg.js
99import { BLOCKCHAIN_IDS } from "dkg.js/constants" ;
10- import { getExplorerUrl } from "../utils" ;
10+ import { getExplorerUrl , validateSparqlQuery } from "../utils" ;
1111
1212export default defineDkgPlugin ( ( ctx , mcp ) => {
13+
1314 async function publishJsonLdAsset (
1415 jsonldRaw : string ,
1516 privacy : "private" | "public" ,
@@ -30,24 +31,6 @@ export default defineDkgPlugin((ctx, mcp) => {
3031 }
3132 }
3233
33- mcp . registerTool (
34- "dkg-get" ,
35- {
36- title : "DKG Knowledge Asset get tool" ,
37- description :
38- "A tool for running a GET operation on OriginTrail Decentralized Knowledge Graph (DKG) and retrieving a specific Knowledge Asset by its UAL (Unique Asset Locator), taking the UAL as input." ,
39- inputSchema : { ual : z . string ( ) } ,
40- } ,
41- async ( { ual } ) => {
42- const getAssetResult = await ctx . dkg . asset . get ( ual ) ;
43- return {
44- content : [
45- { type : "text" , text : JSON . stringify ( getAssetResult , null , 2 ) } ,
46- ] ,
47- } ;
48- } ,
49- ) ;
50-
5134 const ualCompleteOptions : Record < string , CompleteResourceTemplateCallback > = {
5235 blockchainName : ( val ) =>
5336 ( Object . values ( BLOCKCHAIN_IDS ) as string [ ] ) . reduce < string [ ] > (
@@ -77,8 +60,6 @@ export default defineDkgPlugin((ctx, mcp) => {
7760 } ,
7861 [ ] ,
7962 ) ,
80- // TODO: List possible blockchain contract addresses for v8 and v6
81- // blockchainAddress: (val, ctx) =>...
8263 } ;
8364
8465 mcp . registerResource (
@@ -179,4 +160,86 @@ export default defineDkgPlugin((ctx, mcp) => {
179160 } ;
180161 } ,
181162 ) ;
163+
164+ mcp . registerTool (
165+ "dkg-sparql-query" ,
166+ {
167+ title : "DKG SPARQL Query Tool" ,
168+ description :
169+ "Execute SPARQL queries on the OriginTrail Decentralized Knowledge Graph (DKG). " +
170+ "Takes a SPARQL query as input and returns the query results from the DKG. " +
171+ "Supports SELECT and CONSTRUCT queries." ,
172+ inputSchema : {
173+ query : z
174+ . string ( )
175+ . describe ( "SPARQL query to execute on the DKG (SELECT or CONSTRUCT)" ) ,
176+ } ,
177+ } ,
178+ async ( { query } ) => {
179+ // Validate query syntax
180+ const validation = validateSparqlQuery ( query ) ;
181+
182+ if ( ! validation . valid ) {
183+ console . error ( "Invalid SPARQL query:" , validation . error ) ;
184+ return {
185+ content : [
186+ {
187+ type : "text" ,
188+ text : `❌ Invalid SPARQL query: ${ validation . error } \n\nPlease check your query syntax and try again.` ,
189+ } ,
190+ ] ,
191+ } ;
192+ }
193+
194+ // Use validated query type
195+ const queryType = validation . queryType || "SELECT" ;
196+
197+ try {
198+ console . log ( `Executing SPARQL ${ queryType } query...` ) ;
199+ const queryResult = await ctx . dkg . graph . query ( query , queryType ) ;
200+
201+ const resultText = JSON . stringify ( queryResult , null , 2 ) ;
202+
203+ return {
204+ content : [
205+ {
206+ type : "text" ,
207+ text : `✅ Query executed successfully\n\n**Results:**\n\`\`\`json\n${ resultText } \n\`\`\`` ,
208+ } ,
209+ ] ,
210+ } ;
211+ } catch ( error ) {
212+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
213+ console . error ( "Error executing SPARQL query:" , errorMessage ) ;
214+
215+ return {
216+ content : [
217+ {
218+ type : "text" ,
219+ text : `❌ Error executing SPARQL query:\n\n${ errorMessage } \n\nPlease check your query and try again.` ,
220+ } ,
221+ ] ,
222+ } ;
223+ }
224+ } ,
225+ ) ;
226+
227+ mcp . registerTool (
228+ "dkg-get" ,
229+ {
230+ title : "DKG Knowledge Asset get tool" ,
231+ description :
232+ "A tool for running a GET operation on OriginTrail Decentralized Knowledge Graph (DKG) and retrieving a specific Knowledge Asset by its UAL (Unique Asset Locator), taking the UAL as input." ,
233+ inputSchema : { ual : z . string ( ) } ,
234+ } ,
235+ async ( { ual } ) => {
236+ const getAssetResult = await ctx . dkg . asset . get ( ual ) ;
237+ return {
238+ content : [
239+ { type : "text" , text : JSON . stringify ( getAssetResult , null , 2 ) } ,
240+ ] ,
241+ } ;
242+ } ,
243+ ) ;
244+
182245} ) ;
0 commit comments