Skip to main content
Glama

Wikidata SPARQL Query

wikidata_sparql_query
Read-only

Execute a SPARQL SELECT query against the Wikidata Query Service. Full graph power: multi-hop traversals, aggregations, subqueries, OPTIONAL, FILTER, UNION, BIND. Standard Wikidata prefixes (wd:, wdt:, p:, ps:, pq:, wikibase:, bd:, rdfs:, schema:) are auto-injected. The wikibase:label SERVICE is also auto-injected when the query references a ?Label, ?Description, or ?AltLabel variable and declares no label SERVICE of its own — so you can use ?itemLabel without writing the boilerplate. Scholarly articles, and every triple they own (DOI, PMID, authorship), live in a separate graph reachable only inside SERVICE wdsubgraph:scholarly_articles { … } (wdsubgraph: is predeclared). The injected label SERVICE runs on the main graph, so read a scholarly article's label inside that block, with BIND(?paperLabel AS ?paperLabel) and a label SERVICE there; a query that writes a label SERVICE gets none injected, so add a top-level one for main-graph labels. Hard server timeout is 60s; use LIMIT to keep queries fast. Each call returns at most 4,000 rows; a larger result returns its first rows with truncated, shown, and cap in place of totalCount — narrow the WHERE clause or aggregate in the query, or, for a result not much larger than that, page with ORDER BY plus LIMIT and OFFSET. Bindings use the SPARQL 1.1 JSON format: each value is { type, value, "xml:lang"?, datatype? }. Use wikidata_get_labels to humanize QID results from this tool.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "query": "<query>"
    },
    "name": "wikidata_sparql_query"
  }
}

Input Schema

NameRequiredDescriptionDefault
queryYesSPARQL SELECT query. Any other form — ASK, CONSTRUCT, DESCRIBE, an update, or plain search text — is rejected before it is sent. Standard prefixes (wd:, wdt:, p:, ps:, pq:, wikibase:, bd:, rdfs:, schema:) are injected automatically. Example: SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146. } LIMIT 10
timeoutNoClient-side timeout in seconds (1–55). Capped at 55s — the Wikidata server hard limit is 60s.
languageNoWikidata language code, or a comma-separated fallback list tried in order (e.g., "en", "de", "fr,en"), for the injected wikibase:label SERVICE — the language of ?<var>Label, ?<var>Description, and ?<var>AltLabel variables. The list falls back to the multilingual ("mul") label last unless it already names mul. Case-insensitive; "_" is read as "-"; blank means "en".en

Output Schema

NameRequiredDescriptionDefault
capNoThe row ceiling that truncated the result.
errorNoPresent when the call failed. Absent on success.
shownNoNumber of rows returned when truncated.
noticeNoPresent when truncated (how to page or aggregate for the rest) or when no rows matched (where else to look).
resultsNoResult bindings in query order: every row, or the first 4,000 when the result has more (truncated). Each row maps variable names to binding objects with { type, value, "xml:lang"?, datatype? } fields.
truncatedNoTrue when the result had more than 4,000 rows: only the first 4,000 were read and returned.
variablesNoVariable names returned by the SELECT clause.
totalCountNoNumber of rows the query produced. Present only when the whole result was read: absent when truncated.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed10 schema fields changed
    • changedOutput schema / anyOf
      Previous value: -[
      -  {
      -    "not": {
      -      "required": [
      -        "error"
      -      ]
      -    },
      -    "required": [
      -      "results",
      -      "variables",
      -      "truncated",
      -      "totalCount"
      -    ]
      -  },
      -  {
      -    "required": [
      -      "error"
      -    ]
      -  }
      -]New value: +[
      +  {
      +    "not": {
      +      "required": [
      +        "error"
      +      ]
      +    },
      +    "required": [
      +      "results",
      +      "variables"
      +    ]
      +  },
      +  {
      +    "required": [
      +      "error"
      +    ]
      +  }
      +]
    • addedOutput schema / properties / cap
      Added value: +{
      +  "description": "The row ceiling that truncated the result.",
      +  "type": "number"
      +}
    • changedOutput schema / properties / error / properties / data / properties / reason / description
      Previous value: -"Machine-readable failure mode. Declared by this tool: `not_select_query`: The query is not a SELECT query: an ASK, CONSTRUCT, DESCRIBE, or update form, or plain text. `parse_error`: SPARQL syntax error — the query could not be parsed. `timeout`: Query exceeded the client-side timeout. `throttled`: Wikidata SPARQL endpoint is rate-limited (60 req/min, 5 concurrent per IP). Other values are possible when a failure originates below the handler."New value: +"Machine-readable failure mode. Declared by this tool: `not_select_query`: The query is not a SELECT query: an ASK, CONSTRUCT, DESCRIBE, or update form, or plain text. `parse_error`: SPARQL syntax error — the query could not be parsed. `timeout`: The query ran past its client-side timeout, or past the Query Service's own 60s limit. `throttled`: The Wikidata SPARQL endpoint is rate-limiting this server: each client (user agent and IP) gets 60 seconds of query processing time per 60 seconds, and each IP at most 5 parallel queries. `result_too_large`: The result body is larger than the 8 MiB one call reads. Other values are possible when a failure originates below the handler."
    • changedOutput schema / properties / error / properties / data / properties / reason / examples
      Previous value: -[
      -  "not_select_query",
      -  "parse_error",
      -  "timeout",
      -  "throttled"
      -]New value: +[
      +  "not_select_query",
      +  "parse_error",
      +  "timeout",
      +  "throttled",
      +  "result_too_large"
      +]
    • changedOutput schema / properties / notice / description
      Previous value: -"Present when the result was cut to fit the response budget (how to page or aggregate for the rest), or when no rows matched (check query logic or broaden filters)."New value: +"Present when truncated (how to page or aggregate for the rest) or when no rows matched (where else to look)."
    • changedOutput schema / properties / results / description
      Previous value: -"Result bindings: every row when the result fits the 64,000-byte response budget, otherwise the leading rows that fit (returnedCount of totalCount). Each row maps variable names to binding objects with { type, value, \"xml:lang\"?, datatype? } fields."New value: +"Result bindings in query order: every row, or the first 4,000 when the result has more (truncated). Each row maps variable names to binding objects with { type, value, \"xml:lang\"?, datatype? } fields."
    • removedOutput schema / properties / returnedCount
      Removed value: -{
      -  "description": "Number of rows in results — fewer than totalCount when the 64,000-byte response budget cut the result.",
      -  "type": "number"
      -}
    • addedOutput schema / properties / shown
      Added value: +{
      +  "description": "Number of rows returned when truncated.",
      +  "type": "number"
      +}
    • changedOutput schema / properties / totalCount / description
      Previous value: -"Number of rows the query produced, before any response-budget cut."New value: +"Number of rows the query produced. Present only when the whole result was read: absent when truncated."
    • changedOutput schema / properties / truncated / description
      Previous value: -"True when the query produced 10,000 rows (totalCount) — the Wikidata/Blazegraph server-side cap — indicating the result set was likely truncated upstream. False otherwise. Add a LIMIT clause to avoid hitting the cap. Independent of the response-budget cut, which returnedCount reports. Note: the SPARQL endpoint does not expose an explicit truncation flag; this is an inferred signal."New value: +"True when the result had more than 4,000 rows: only the first 4,000 were read and returned."
  2. Changed10 schema fields changed
    • changedInput schema / properties / language / description
      Previous value: -"Language for the wikibase:label SERVICE (e.g., \"en\", \"de\"). Controls the language of ?<var>Label variables. Set to \"\" to suppress label SERVICE injection."New value: +"Wikidata language code, or a comma-separated fallback list tried in order (e.g., \"en\", \"de\", \"fr,en\"), for the injected wikibase:label SERVICE — the language of ?<var>Label, ?<var>Description, and ?<var>AltLabel variables. The list falls back to the multilingual (\"mul\") label last unless it already names mul. Case-insensitive; \"_\" is read as \"-\"; blank means \"en\"."
    • addedInput schema / properties / language / pattern
      Added value: +"^\\s*(?:[A-Za-z]{2,8}(?:[-_][A-Za-z0-9]{1,9})*(?:\\s*,\\s*[A-Za-z]{2,8}(?:[-_][A-Za-z0-9]{1,9})*)*\\s*)?$"
    • changedInput schema / properties / query / description
      Previous value: -"SPARQL SELECT query. Must be a SELECT query (not CONSTRUCT/DESCRIBE/ASK). Standard prefixes (wd:, wdt:, p:, ps:, pq:, wikibase:, bd:) are injected automatically. Example: SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146. } LIMIT 10"New value: +"SPARQL SELECT query. Any other form — ASK, CONSTRUCT, DESCRIBE, an update, or plain search text — is rejected before it is sent. Standard prefixes (wd:, wdt:, p:, ps:, pq:, wikibase:, bd:, rdfs:, schema:) are injected automatically. Example: SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146. } LIMIT 10"
    • changedOutput schema / properties / error / properties / data / properties / reason / description
      Previous value: -"Machine-readable failure mode. Declared by this tool: `parse_error`: SPARQL syntax error — the query could not be parsed. `timeout`: Query exceeded the client-side timeout. `throttled`: Wikidata SPARQL endpoint is rate-limited (60 req/min, 5 concurrent per IP). Other values are possible when a failure originates below the handler."New value: +"Machine-readable failure mode. Declared by this tool: `not_select_query`: The query is not a SELECT query: an ASK, CONSTRUCT, DESCRIBE, or update form, or plain text. `parse_error`: SPARQL syntax error — the query could not be parsed. `timeout`: Query exceeded the client-side timeout. `throttled`: Wikidata SPARQL endpoint is rate-limited (60 req/min, 5 concurrent per IP). Other values are possible when a failure originates below the handler."
    • changedOutput schema / properties / error / properties / data / properties / reason / examples
      Previous value: -[
      -  "parse_error",
      -  "timeout",
      -  "throttled"
      -]New value: +[
      +  "not_select_query",
      +  "parse_error",
      +  "timeout",
      +  "throttled"
      +]
    • changedOutput schema / properties / notice / description
      Previous value: -"Recovery hint when no results returned — suggests checking query logic or broadening filters. Absent on non-empty results."New value: +"Present when the result was cut to fit the response budget (how to page or aggregate for the rest), or when no rows matched (check query logic or broaden filters)."
    • changedOutput schema / properties / results / description
      Previous value: -"Array of result bindings. Each row maps variable names to binding objects with { type, value, \"xml:lang\"?, datatype? } fields."New value: +"Result bindings: every row when the result fits the 64,000-byte response budget, otherwise the leading rows that fit (returnedCount of totalCount). Each row maps variable names to binding objects with { type, value, \"xml:lang\"?, datatype? } fields."
    • addedOutput schema / properties / returnedCount
      Added value: +{
      +  "description": "Number of rows in results — fewer than totalCount when the 64,000-byte response budget cut the result.",
      +  "type": "number"
      +}
    • changedOutput schema / properties / totalCount / description
      Previous value: -"Number of result rows returned."New value: +"Number of rows the query produced, before any response-budget cut."
    • changedOutput schema / properties / truncated / description
      Previous value: -"True when the row count equals 10,000 — the Wikidata/Blazegraph server-side cap — indicating the result set was likely truncated. False otherwise. Add a LIMIT clause to avoid hitting the cap. Note: the SPARQL endpoint does not expose an explicit truncation flag; this is an inferred signal."New value: +"True when the query produced 10,000 rows (totalCount) — the Wikidata/Blazegraph server-side cap — indicating the result set was likely truncated upstream. False otherwise. Add a LIMIT clause to avoid hitting the cap. Independent of the response-budget cut, which returnedCount reports. Note: the SPARQL endpoint does not expose an explicit truncation flag; this is an inferred signal."
  3. Changed6 schema fields changed
    • changedInput schema / $schema
      Previous value: -"http://json-schema.org/draft-07/schema#"New value: +"https://json-schema.org/draft/2020-12/schema"
    • addedInput schema / additionalProperties
      Added value: +false
    • changedOutput schema / $schema
      Previous value: -"http://json-schema.org/draft-07/schema#"New value: +"https://json-schema.org/draft/2020-12/schema"
    • addedOutput schema / anyOf
      Added value: +[
      +  {
      +    "not": {
      +      "required": [
      +        "error"
      +      ]
      +    },
      +    "required": [
      +      "results",
      +      "variables",
      +      "truncated",
      +      "totalCount"
      +    ]
      +  },
      +  {
      +    "required": [
      +      "error"
      +    ]
      +  }
      +]
    • addedOutput schema / properties / error
      Added value: +{
      +  "additionalProperties": {},
      +  "description": "Present when the call failed. Absent on success.",
      +  "properties": {
      +    "code": {
      +      "description": "JSON-RPC error code for this failure.",
      +      "maximum": 9007199254740991,
      +      "minimum": -9007199254740991,
      +      "type": "integer"
      +    },
      +    "data": {
      +      "additionalProperties": {},
      +      "properties": {
      +        "reason": {
      +          "description": "Machine-readable failure mode. Declared by this tool: `parse_error`: SPARQL syntax error — the query could not be parsed. `timeout`: Query exceeded the client-side timeout. `throttled`: Wikidata SPARQL endpoint is rate-limited (60 req/min, 5 concurrent per IP). Other values are possible when a failure originates below the handler.",
      +          "examples": [
      +            "parse_error",
      +            "timeout",
      +            "throttled"
      +          ],
      +          "type": "string"
      +        },
      +        "recovery": {
      +          "additionalProperties": {},
      +          "description": "Actionable next step for the caller.",
      +          "properties": {
      +            "hint": {
      +              "type": "string"
      +            }
      +          },
      +          "required": [
      +            "hint"
      +          ],
      +          "type": "object"
      +        },
      +        "retryable": {
      +          "description": "Whether retrying may succeed.",
      +          "type": "boolean"
      +        }
      +      },
      +      "type": "object"
      +    },
      +    "message": {
      +      "description": "Human-readable description of what went wrong.",
      +      "type": "string"
      +    }
      +  },
      +  "required": [
      +    "code",
      +    "message"
      +  ],
      +  "type": "object"
      +}
    • removedOutput schema / required
      Removed value: -[
      -  "results",
      -  "variables",
      -  "truncated",
      -  "totalCount"
      -]
  4. Changed1 schema field changed
    • changedOutput schema / properties / truncated / description
      Previous value: -"True when the endpoint returned a partial result set due to server-side memory limits. False when the full result was returned. Add a LIMIT clause to avoid truncation on large queries."New value: +"True when the row count equals 10,000 — the Wikidata/Blazegraph server-side cap — indicating the result set was likely truncated. False otherwise. Add a LIMIT clause to avoid hitting the cap. Note: the SPARQL endpoint does not expose an explicit truncation flag; this is an inferred signal."
  5. Changed4 schema fields changed
    • addedOutput schema / properties / notice
      Added value: +{
      +  "description": "Recovery hint when no results returned — suggests checking query logic or broadening filters. Absent on non-empty results.",
      +  "type": "string"
      +}
    • removedOutput schema / properties / rowCount
      Removed value: -{
      -  "description": "Number of result rows.",
      -  "type": "number"
      -}
    • addedOutput schema / properties / totalCount
      Added value: +{
      +  "description": "Number of result rows returned.",
      +  "type": "number"
      +}
    • changedOutput schema / required
      Previous value: -[
      -  "results",
      -  "variables",
      -  "rowCount",
      -  "truncated"
      -]New value: +[
      +  "results",
      +  "variables",
      +  "truncated",
      +  "totalCount"
      +]
  6. First observed

TDQS

A4.4/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Beyond the readOnly/openWorld annotations, the description discloses the 60s hard server timeout, the 4,000-row return cap and the truncated/shown/cap response shape when exceeded, plus concrete remediation (narrow WHERE, aggregate, or page with ORDER BY/LIMIT/OFFSET). It also flags a non-obvious behavioral boundary: scholarly-article triples live in a separate SERVICE-reachable graph that the auto-injected label SERVICE cannot see.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Purpose is front-loaded and each block (capability, injection, scholarly graph, timeout, row cap, return format) is substantive rather than filler. It is noticeably dense, and the scholarly-article paragraph is written as tangled prose that takes a re-read, which costs it the top score.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a high-complexity raw-query tool this covers the operational essentials an agent needs: accepted query form, auto-injected prefixes and label SERVICE, the separate scholarly-articles graph, timeout limits, row caps, and the JSON bindings shape (an output schema also exists). Little material is left for the agent to discover by failing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so all three parameters are already documented in detail. The description adds contextual value by explaining the auto-injection of prefixes and the label SERVICE (and the exception when the caller writes its own label SERVICE), but it largely restates what the schema's query and language descriptions already say rather than extending parameter meaning.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The first sentence gives a specific verb and resource ('Execute a SPARQL SELECT query against the Wikidata Query Service') and immediately scopes the capability to raw graph traversal, aggregations, and subqueries, which is clearly distinct from the structured accessors among the siblings (get_entity, get_statements, get_labels). It also names wikidata_get_labels as the follow-up tool, reinforcing the boundary.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Guidance is clear about the kind of work this tool is for ('full graph power: multi-hop traversals, aggregations...') and about post-processing results via wikidata_get_labels, and it explicitly rejects ASK/CONSTRUCT/DESCRIBE/update/free-text forms. It stops short of stating when to prefer a sibling like wikidata_get_entity over writing SPARQL, so the exclusion side is only implied.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Try in Browser

Glama MCP Gateway

Add one secure layer between your agents and this server.