Kagi Fastmcp Server
FastMCP server for Kagi search with persistent authentication - authenticate once, search for weeks. Perfect for Claude Desktop & Cursor.
Ask AI about Kagi Fastmcp Server
Powered by Claude Β· Grounded in docs
I know everything about Kagi Fastmcp Server. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
Kagi FastMCP Server π
A powerful FastMCP (Model Context Protocol) server for searching Kagi.com and fetching web pages with persistent authentication and browser automation. Perfect for Claude Desktop, Cursor, and other MCP clients.
π― Why This Implementation?
Key Innovation: Single Sign-On with Persistent Cookies
- β Authenticate once, cookies persist for ~30 days
- β No re-authentication on server restarts
- β Perfect for MCP clients that frequently start/stop servers
- β Built on pure FastMCP 2.0 for optimal performance
β¨ Key Features
- π Single Sign-On: Authenticate once, use for weeks
- πΎ Persistent Cookie Storage: Saves to
~/.kagi_mcp/cookies.json - π FastMCP Native: Built specifically for MCP clients
- π Two Powerful Tools:
search: Search Kagi.com with full resultsfetch_webpage: Fetch and convert pages to markdown
- π Progress Reporting: Real-time updates via MCP Context
- π Bearer Token Auth: Secure client authentication
- ποΈ Modular Architecture: Clean, maintainable codebase
- π§ͺ Comprehensive Testing: Unit + integration tests
- π³ No API Credits: Uses your Kagi Professional subscription
π Quick Start
Prerequisites
# Install dependencies
uv sync
# Install Playwright browsers
uv run playwright install chromium
Environment Setup
# Required
export KAGI_TOKEN="your_kagi_token" # From kagi.com
export ACCESS_TOKEN="your_access_token" # For MCP client auth
# Optional
export KAGIAPI_PORT=8000 # Default port
export LOGGING_LEVEL=INFO # Log level
Run the Server
# HTTP server (for testing)
uv run python run_server.py
# MCP server (for Claude Desktop/Cursor)
uv run python main.py --stdio
π Usage
With FastMCP Client
from fastmcp import Client
from fastmcp.client.auth import BearerAuth
async with Client(
"http://localhost:8000/mcp",
auth=BearerAuth(token="your_access_token")
) as client:
# Search Kagi
results = await client.call_tool("search", {
"query": "python fastmcp tutorial"
})
print(f"Found {results.structured_content['total_found']} results")
# Fetch webpage
content = await client.call_tool("fetch_webpage", {
"url": "https://example.com"
})
print(f"Fetched {len(content.structured_content)} characters")
With Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"kagi-search": {
"command": "uv",
"args": ["run", "python", "/path/to/kagiapi/main.py", "--stdio"],
"env": {
"KAGI_TOKEN": "your_kagi_token",
"ACCESS_TOKEN": "your_access_token"
}
}
}
}
With Cursor
- Install as MCP server in Cursor settings
- Use the tools directly in your coding workflow
ποΈ Architecture
kagiapi/
βββ services/
β βββ browser.py # Cookie-persistent browser service
βββ models/
β βββ types.py # Pydantic data models
βββ auth/
β βββ providers.py # Bearer token authentication
βββ tests/
β βββ test_integration.py # Comprehensive test suite
β βββ auth_helper.py # Interactive auth for tests
βββ server.py # FastMCP server with tools
βββ main.py # Entry point (supports --stdio)
βββ run_server.py # HTTP server runner
βββ README_FASTMCP.md # Detailed technical docs
π Authentication Flow
- First Run: Server authenticates with Kagi, saves cookies to disk
- Subsequent Runs: Loads existing cookies, validates them
- If Valid: Skips authentication entirely (no browser launch needed!)
- If Expired: Re-authenticates automatically (rare, ~30 days)
Cookie Storage: ~/.kagi_mcp/cookies.json (secure permissions: 600)
π§ͺ Testing
# Test all components
uv run python test_initialization.py
# Integration tests (requires auth)
uv run python tests/auth_helper.py # First time only
uv run python tests/test_integration.py
# Test live server
uv run python run_server.py & # Start server
uv run python test_live_server.py # Test client
π FastAPI vs FastMCP Comparison
| Feature | Original FastAPI | New FastMCP |
|---|---|---|
| Authentication | Every server start | Once every ~30 days |
| Cookie Storage | In-memory only | Persistent to disk |
| Architecture | Monolithic | Modular services |
| MCP Features | Basic | Full Context/Progress |
| Framework | FastAPI + custom MCP | Pure FastMCP 2.0 |
| Client Support | HTTP only | MCP native |
| Testing | Basic | Comprehensive |
π§ Configuration
Environment Variables
KAGI_TOKEN(required): Your Kagi authentication tokenACCESS_TOKEN(required): Bearer token for MCP client authKAGIAPI_PORT(optional): HTTP server port (default: 8000)LOGGING_LEVEL(optional): Log level (default: INFO)
Cookie Management
- Location:
~/.kagi_mcp/cookies.json - Permissions: 600 (user read/write only)
- Lifetime: ~30 days (validated on startup)
- Format: JSON with cookies + timestamp
π Troubleshooting
Server Won't Start
# Missing KAGI_TOKEN
export KAGI_TOKEN="your_token_here"
# Permission issues
chmod 600 ~/.kagi_mcp/cookies.json
# Clear stale cookies
rm ~/.kagi_mcp/cookies.json
Authentication Issues
# Re-authenticate manually
uv run python tests/auth_helper.py
# Verify token is valid
curl "https://kagi.com/search?token=your_token"
π Performance Benefits
- Startup Time: ~3x faster after first auth (no browser launch)
- Memory Usage: Lower (no persistent browser instances)
- Network: Reduced (cookie reuse vs repeated auth)
- UX: Instant tool availability for MCP clients
π€ Contributing
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Run the test suite:
uv run python tests/test_integration.py - Submit a Pull Request
π Documentation
- FastMCP Documentation: Learn about FastMCP
- README_FASTMCP.md: Detailed technical documentation
- Model Context Protocol: MCP specification
βοΈ License
Apache 2.0. See LICENSE.
π Credits
FastMCP Implementation: Built with modern FastMCP 2.0 architecture
Original FastAPI Version: Based on yuchanns/kagiapi
Authenticate once, search for weeks. Built for the MCP ecosystem. π
