ryge/wikidata-mcp
If you are the rightful owner of wikidata-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
A Model Context Protocol (MCP) server that provides access to Wikidata's SPARQL endpoint, enabling AI assistants to query the world's largest free knowledge base for facts and structured information.
Wikidata SPARQL MCP Server
A Model Context Protocol (MCP) server that provides access to Wikidata's SPARQL endpoint, enabling AI assistants to query the world's largest free knowledge base for facts and structured information.
Features
- 🔍 SPARQL Query Execution: Execute custom SPARQL queries against Wikidata's knowledge base
- 🔎 Entity Search: Search for Wikidata entities by name across multiple languages
- 📊 Entity Information: Get detailed information about specific Wikidata entities
- 📚 Example Queries: Built-in collection of useful SPARQL query examples
- 🌐 Dual Transport Support: Works with both stdio and HTTP (SSE) transports
- ⚡ Async Support: Built with modern async/await patterns for optimal performance
What is Wikidata?
Wikidata is a free, collaborative knowledge base that can be read and edited by both humans and machines. It contains over 100 million items with structured data about people, places, concepts, and more. The SPARQL endpoint allows you to query this vast knowledge base using semantic queries.
Installation
Prerequisites
- Python 3.10 or higher
- pip or uv package manager
Install Dependencies
Using pip:
pip install -r requirements.txt
Using uv (recommended):
uv pip install -r requirements.txt
Usage
Running with stdio (default)
For use with Claude Desktop or other MCP clients:
python server.py
Running with Python virtual environments
If you're using a Python virtual environment (recommended), you can use the provided shell script that automatically activates the virtual environment, runs the server, and deactivates:
./server.sh
This script assumes your virtual environment is located at .venv/ in the project directory. If you use a different location, you'll need to modify server.sh accordingly.
Running with HTTP transport
For web-based access or testing:
python server.py --http
The server will start on http://localhost:8000
Configuration for Claude Desktop
Add the following to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"wikidata": {
"command": "python",
"args": [
"/Users/anr/Documents/Claude/wikidata-mcp/server.py"
]
}
}
}
Or using uv:
{
"mcpServers": {
"wikidata": {
"command": "uv",
"args": [
"run",
"--with",
"fastmcp",
"--with",
"httpx",
"/Users/anr/Documents/Claude/wikidata-mcp/server.py"
]
}
}
}
Available Tools
1. sparql_query
Execute SPARQL queries against Wikidata.
Parameters:
query(string, required): The SPARQL query to executetimeout(int, optional): Query timeout in seconds (default: 30, max: 60)format(string, optional): Response format - 'json' or 'xml' (default: 'json')
Example:
await sparql_query("""
SELECT ?person ?personLabel WHERE {
?person wdt:P31 wd:Q5 .
?person wdt:P19 wd:Q78 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 10
""")
2. search_entities
Search for Wikidata entities by name.
Parameters:
search_term(string, required): The term to search forlanguage(string, optional): Language code (default: 'en')limit(int, optional): Max results (default: 10, max: 50)type(string, optional): Filter by 'item' or 'property'
Example:
await search_entities("Albert Einstein", language="en", limit=5)
3. get_entity_info
Get detailed information about a specific Wikidata entity.
Parameters:
entity_id(string, required): The Wikidata ID (e.g., 'Q937')language(string, optional): Language code (default: 'en')
Example:
await get_entity_info("Q937", language="en") # Albert Einstein
Available Resources
1. wikidata://examples/queries
Get example SPARQL queries for common use cases.
2. wikidata://help/prefixes
Get documentation about common SPARQL prefixes used in Wikidata.
Example Queries
Find Nobel Prize Winners
SELECT ?person ?personLabel ?awardYear WHERE {
?person wdt:P166 wd:Q38104 .
OPTIONAL { ?person p:P166 [ps:P166 wd:Q38104; pq:P585 ?awardYear] . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?awardYear)
LIMIT 50
Get Properties of an Entity
SELECT ?property ?propertyLabel ?value ?valueLabel WHERE {
wd:Q937 ?p ?value .
?property wikibase:directClaim ?p .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 50
Find Countries and Capitals
SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE {
?country wdt:P31 wd:Q3624078 .
?country wdt:P36 ?capital .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?countryLabel
Common Wikidata Properties
- P31: instance of
- P279: subclass of
- P19: place of birth
- P20: place of death
- P569: date of birth
- P570: date of death
- P166: award received
- P106: occupation
- P27: country of citizenship
- P36: capital
Common Wikidata Entities
- Q5: human
- Q515: city
- Q6256: country
- Q3624078: sovereign state
- Q38104: Nobel Prize in Physics
Troubleshooting
Query Timeout
If queries timeout, try:
- Adding a
LIMITclause to reduce results - Simplifying complex queries
- Increasing the timeout parameter (max 60 seconds)
Empty Results
- Verify entity IDs are correct (e.g., Q937, not 937)
- Check that properties exist for the entity
- Ensure SPARQL syntax is correct
- Add the
SERVICE wikibase:labelclause for human-readable labels
Connection Issues
- Verify internet connectivity
- Check if query.wikidata.org is accessible
- Ensure firewall isn't blocking requests
Testing
You can test the server using the MCP Inspector:
# Install MCP Inspector
npm install -g @modelcontextprotocol/inspector
# Start the server
python server.py
# In another terminal, start the inspector
mcp-inspector
Resources
- Wikidata Query Service
- SPARQL Tutorial
- Wikidata Property Browser
- FastMCP Documentation
- Model Context Protocol
License
This project is licensed under the BSD 2-Clause License - see the file for details.
This MCP server is provided for use with Wikidata's public SPARQL endpoint. Please respect Wikidata's query limits and terms of service.
Contributing
Feel free to enhance this server with additional tools and features:
- Support for federated queries
- Query result caching
- Query optimization suggestions
- Additional entity relationship tools
- Batch query support
Support
For issues with:
- This MCP server: Check the server logs and error messages
- Wikidata queries: Consult Wikidata SPARQL documentation
- MCP protocol: See MCP documentation