@comunica/mcp-sparql
v1.0.0
Published
An MCP server for executing SPARQL queries over decentralized RDF knowledge graphs on the Web
Readme
Comunica MCP SPARQL
Comunica MCP SPARQL is an MCP server for allowing AI agents to execute SPARQL queries over decentralized RDF knowledge graphs on the Web.
It's main distinguishing features are the following:
- Improves the accuracy of your AI agent's answers by leveraging the power of SPARQL and Knowledge Graphs.
- Execute SPARQL 1.2 queries over one or more knowledge graphs on the Web.
- Agents can pass one or more Knowledge Graph URLs together with the SPARQL query, making it not hard-coupled to one specific Knowledge Graph.
- Federated querying over heterogeneous interfaces, such as RDF Linked Data files, SPARQL endpoints, Triple Pattern Fragments, or Solid data pods.
- Easy to set up, and connects to public or private/internal Knowledge Graphs.
Learn more about Comunica on our website.
Internally, this is a Comunica module that is configured with modules to execute SPARQL queries through MCP.
Supported by
Comunica is a community-driven project, sustained by the Comunica Association. If you are using Comunica, becoming a sponsor or member is a way to make Comunica sustainable in the long-term.
Our top sponsors are shown below!
Usage examples
After connecting this MCP server to your AI agent (see some examples on how to do this below), your agent can SPARQL query any Knowledge Graph that is accessible to you.
For example, use it to ask:
Use Comunica SPARQL to determine what movies both Brad Pitt and Leonardo DiCaprio both play in.
This will produce two movies: Once Upon a Time in Hollywood and The Audition. Without SPARQL, the answer on Claude Desktop (Sonnet 4.5) is incomplete, as only a single movie is returned.
Prompt suggestions
If you want your agent to always use SPARQL for higher accuracy in answers, you can tell it the following:
When looking up data, always prefer looking them up over Knowledge Graphs via SPARQL, as this is more precise. If data needs to be combined from multiple Knowledge Graphs, do so using a virtually integrated SPARQL query towards multiple URLs pointing to Knowledge Graphs. You are responsible for selecting relevant sources for the SPARQL query, for which you can consider SPARQL endpoints, TPF interfaces, and Linked Data (in any RDF serialization). Also take into account that many websites contain RDF data. So if you need to lookup data about something, and you know the relevant website(s), try querying those websites using SPARQL. Since SPARQL queries can take a while to execute, start producing an approximate answer yourself and show it to me if the SPARQL query takes more than 1 second, but then make the answer more concrete based on the SPARQL query once it finalized, as it will be more accurate. Since you will probably query over Wikidata and DBpedia often, prefer authoritative Knowledge Graphs for domain-specific data if they exist.
After this, SPARQL will be used for questions such as:
- "What is the time zone of Salt Lake City?"
- "What movies do both Brad Pitt and Leonardo DiCaprio play in?"
- "What are the largest cities in Canada?"
- "Give me Ruben Taelman's main interests."
- "What are some proteins associated with Alzheimer's disease, according to a protein knowledge graph?"
- "What are the 10 longest streets in Belgium?"
Limitations
Most models seem to prefer Wikidata and DBpedia for most questions. If you want it to query another Knowledge Graph, you need to be explicit. When performing federated queries, endpoint URLs passed to the MCP server are not always accurate.
Installation
Comunica requires Node.JS 14.0 or higher and is tested on OSX and Linux.
The easiest way to install the client is by installing it from NPM as follows:
$ [sudo] npm install -g @comunica/mcp-sparqlAlternatively, you can install from the latest GitHub sources.
For this, please refer to the README of the Comunica monorepo.
If you do so, the following examples require replacing comunica-mcp-sparql with node engines/mcp-sparql/bin/mcp.js.
Connect this MCP server to your agent
Below, a non-exhaustive list of examples is given to connect this MCP server to your agent.
Claude Desktop
After installing, you can run the MCP server in two modes:
Stdio Mode (Recommended for Claude Desktop)
With stdio mode, the MCP server communicates directly via standard input/output, which is simpler and doesn't require a network port.
Add the following entry to your claude_desktop_config.json file (can be found via Settings / Developer / Edit Config):
{
"mcpServers": {
"comunica-sparql": {
"command": "npx",
"args": [
"-y",
"@comunica/mcp-sparql",
"--mode",
"stdio"
]
}
}
}HTTP Mode
Alternatively, you can run the MCP server in HTTP mode, which requires starting the server manually first:
$ comunica-mcp-sparql --mode http --port 3123Then, add the following entry to your claude_desktop_config.json:
{
"mcpServers": {
"comunica-sparql": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"http://localhost:3123/mcp",
"--allow-http"
]
}
}
}Then, you can ask Claude something like the following:
Use Comunica SPARQL to determine what movies both Brad Pitt and Leonardo DiCaprio both play in.
Default Sources
You can optionally configure default sources when starting the MCP server. When default sources are provided, the sources parameter is hidden from the tools, and all queries automatically use the configured sources.
This is useful when you want to restrict queries to specific Knowledge Graphs or avoid having the AI agent select sources on its own.
Stdio Mode with Default Sources:
{
"mcpServers": {
"comunica-sparql": {
"command": "npx",
"args": [
"-y",
"@comunica/mcp-sparql",
"--mode",
"stdio",
"https://dbpedia.org/sparql",
"https://query.wikidata.org/sparql"
]
}
}
}HTTP Mode with Default Sources:
$ comunica-mcp-sparql --mode http --port 3123 https://dbpedia.org/sparql https://query.wikidata.org/sparqlSource Type Prefixes:
You can optionally force a source type by prefixing the URL with a type annotation, following the same syntax as the Comunica CLI:
$ comunica-mcp-sparql --mode stdio sparql@https://dbpedia.org/sparql file@/path/to/data.ttl hypermedia@https://fragments.dbpedia.org/2016-04/enThis is useful when the source type is already known to avoid auto-detection overhead.
Claude Code
Stdio Mode (Recommended for Claude Code)
With stdio mode, the MCP server communicates directly via standard input/output, which is simpler and doesn't require a network port.
claude mcp add --transport stdio sparql -- npx -y @comunica/mcp-sparql --mode stdioLearn more in the Claude Code MCP docs, such as for running this on Windows.
HTTP Mode
Alternatively, you can run the MCP server in HTTP mode, which requires starting the server manually first:
$ comunica-mcp-sparql --mode http --port 3123claude mcp add --transport http sparql http://localhost:3123/mcpChatGPT
At the time of writing, ChatGPT only supports HTTP-based MCP servers. So you'll need to run this tool under HTTP mode and expose it to the public Web, possibly combined with a reverse proxy and/or OAuth layer.
$ comunica-mcp-sparql --mode http --port 3123Available Tools
This MCP server provides the following tools:
query_sparql
Execute SPARQL queries over one or more remote sources (SPARQL endpoints, TPF interfaces, or RDF files), which also includes update queries.
Parameters:
query(required): SPARQL query stringsources(required, unless default sources are configured): List of SPARQL endpoint URLs, TPF interface URLs, or Linked Data (RDF) file pathsqueryFormatLanguage(optional): Query language (e.g.,sparql,graphql). Allows you to specify alternative query languages supported by ComunicaqueryFormatVersion(optional): Query language version (e.g.,1.0,1.1,1.2). Specifies the version of the query language to usebaseIRI(optional): Base IRI for resolving relative IRIs in the queryhttpProxy(optional): HTTP proxy URL (e.g.,http://proxy.example.com:8080)httpAuth(optional): HTTP basic authentication in the formatusername:passwordhttpTimeout(optional): HTTP request timeout in millisecondshttpRetryCount(optional): Number of HTTP request retries on failure
Note: When the MCP server is started with default sources, the sources parameter is not available, and all queries automatically use the configured default sources.
query_sparql_rdf
Execute SPARQL queries over a serialized RDF dataset provided as a string (useful for querying Turtle, N-Triples, or other RDF formats directly).
Parameters:
query(required): SPARQL query stringvalue(required): Serialized RDF dataset as a stringmediaType(required): Media type of the serialized RDF dataset (e.g.,text/turtle,application/n-triples,application/ld+json)fileBaseIRI(optional): Base IRI for resolving relative IRIs in the RDF datasetbaseIRI(optional): Base IRI for resolving relative IRIs in the queryqueryFormatLanguage(optional): Query language (e.g.,sparql,graphql). Allows you to specify alternative query languages supported by ComunicaqueryFormatVersion(optional): Query language version (e.g.,1.0,1.1,1.2). Specifies the version of the query language to use
Learn more
This README just shows the tip of the iceberg! Learn more about Comunica's functionalities in the following guides:
