Wikidata API服务器

一个基于模型上下文协议(MCP)实现的Wikidata API服务器,提供搜索实体/属性ID、提取元数据和执行SPARQL查询等功能。

  • by zzaebok
  • Python
  • 本地部署
  • 49 星标
  • 更新于
在 GitHub 查看 zzaebok/mcp-wikidata

使用说明

维基数据MCP服务器

使用模型上下文协议(MCP)的Wikidata API的服务器实现。 该项目提供了与维基数据交互的工具,例如 搜索标识符 (实体和财产), 提取元数据 (标签和说明)以及 执行sparql查询.


安装

通过Smithery安装

通过以下方式自动安装克劳德桌面版维基数据MCP服务器 史密瑟里:

npx -y @smithery/cli install @zzaebok/mcp-wikidata --client claude

手动安装

安装 uv 如果尚未安装。

$ curl -LsSf https://astral.sh/uv/install.sh | sh

然后,安装依赖项。

$ git clone https://github.com/zzaebok/mcp-wikidata.git
$ cd mcp-wikidata
$ uv sync
# if you want to run client example together
$ uv sync --extra example

跑

使用以下命令运行服务器:

$ uv run src/server.py

如果你想用一个简单的客户端代码来测试它( langchain-mcp-adapters),使用以下命令运行客户端:

# in another shell
$ uv run src/client.py

LLM提取有效的实体和属性标识符,执行sparql查询,最后推荐奉俊昊执导的电影。

See the execution output
{
  "messages": [
      HumanMessage(
          content="Can you recommend me a movie directed by Bong Joonho?",
      ),
      AIMessage(
          tool_calls=[
              {
                  "name": "search_entity",
                  "args": {"query": "Bong Joon-ho"},
              }
          ],
      ),
      ToolMessage(
          content="Q495980",
          name="search_entity",
      ),
      AIMessage(
          tool_calls=[
              {
                  "name": "get_properties",
                  "args": {"entity_id": "Q495980"},
              }
          ],
      ),
      ToolMessage(
          content='["P345", "P244", "P214", "P227", ...]',
          name="get_properties",
      ),
      AIMessage(
          tool_calls=[
              {
                  "name": "search_property",
                  "args": {"query": "director"},
              }
          ],
      ),
      ToolMessage(
          content="P57",
          name="search_property",
      ),
      AIMessage(
          tool_calls=[
              {
                  "name": "execute_sparql",
                  "args": {
                      "sparql_query": 'SELECT ?film ?filmLabel WHERE {\n  ?film wdt:P57 wd:Q495980.\n  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }\n} LIMIT 1'
                  },
              }
          ],
      ),
      ToolMessage(
          content='[{"film": {"type": "uri", "value": "http://www.wikidata.org/entity/Q483761"}, "filmLabel": {"xml:lang": "en", "type": "literal", "value": "Mother"}}]',
          name="execute_sparql",
      ),
      AIMessage(
          content='I recommend the movie "Mother," which was directed by Bong Joon-ho.',
      ),
  ]
}

维基数据MCP工具

服务器中实现了以下工具:

工具 说明
search_entity(query: str) 通过查询搜索维基数据实体ID。
search_property(query: str) 通过查询搜索维基数据属性ID。
get_properties(entity_id: str) 获取与给定维基数据实体ID关联的属性
execute_sparql(sparql_query: str) 在维基数据上执行SPARQL查询。
get_metadata(entity_id: str, language: str = "en") 检索给定维基数据实体ID的英文标签和描述

许可证

MIT许可证