首先我们安装工具和 MCP 服务器:
In [ ]:
Copied!
!pip install llama-index-tools-notion mcp fastmcp
!pip install llama-index-tools-notion mcp fastmcp
引入依赖项:
In [ ]:
Copied!
# Import dependencies for Model Context Protocol (MCP) fastMCP server
from typing import Any, Dict, List, Optional
from fastmcp import FastMCP
print("MCP fastMCP server dependencies imported successfully!")
# Import dependencies for Model Context Protocol (MCP) fastMCP server
from typing import Any, Dict, List, Optional
from fastmcp import FastMCP
print("MCP fastMCP server dependencies imported successfully!")
MCP fastMCP server dependencies imported successfully!
使用 API 密钥实例化我们的工具:
In [ ]:
Copied!
# Import and configure LlamaIndex Notion Tool Spec
from llama_index.tools.notion import NotionToolSpec
notion_token = "xxxx"
tool_spec = NotionToolSpec(integration_token=notion_token)
# Import and configure LlamaIndex Notion Tool Spec
from llama_index.tools.notion import NotionToolSpec
notion_token = "xxxx"
tool_spec = NotionToolSpec(integration_token=notion_token)
让我们看看有哪些可用工具:
In [ ]:
Copied!
tools = tool_spec.to_tool_list()
for i, tool in enumerate(tools):
print(f"Tool {i+1}: {tool.metadata.name}")
tools = tool_spec.to_tool_list()
for i, tool in enumerate(tools):
print(f"Tool {i+1}: {tool.metadata.name}")
Tool 1: load_data Tool 2: search_data
现在我们创建并配置 fastMCP 服务器,并注册每个工具:
In [ ]:
Copied!
mcp_server = FastMCP("MCP Agent Tools Server")
# Register the tools from the Notion ToolSpec
for tool in tools:
mcp_server.tool(
name=tool.metadata.name, description=tool.metadata.description
)(tool.real_fn)
mcp_server = FastMCP("MCP Agent Tools Server")
# Register the tools from the Notion ToolSpec
for tool in tools:
mcp_server.tool(
name=tool.metadata.name, description=tool.metadata.description
)(tool.real_fn)
MCP Server configured with tools
现在我们可以运行完整的 MCP 服务器及其工具了!
In [ ]:
Copied!
await mcp_server.run_async(transport="streamable-http")
await mcp_server.run_async(transport="streamable-http")
INFO: Started server process [24668] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:54201 - "POST /mcp/ HTTP/1.1" 200 OK INFO: 127.0.0.1:54201 - "POST /mcp/ HTTP/1.1" 202 Accepted INFO: 127.0.0.1:54203 - "GET /mcp/ HTTP/1.1" 200 OK INFO: 127.0.0.1:54209 - "POST /mcp/ HTTP/1.1" 200 OK
INFO: Shutting down ERROR: ASGI callable returned without completing response. ERROR: Cancel 0 running task(s), timeout graceful shutdown exceeded INFO: Waiting for application shutdown. INFO: Application shutdown complete. INFO: Finished server process [24668]