hn-mcp
   
HN线程有500多条评论,嵌套了10层。你的AI代理需要在不打开上下文窗口的情况下读取它们。
hn-mcp 是一个MCP服务器,它使AI代理能够完全访问Hacker News——完整的评论树、搜索和用户配置文件——并具有深度控制,因此他们可以逐步探索,而不是一次获取所有内容。
特性
- 完整的评论树 --无深度限制,无截断
- 深度控制 --只获取顶级注释或整个树
- 智能修剪 —
reply_count在临界点,让代理决定扩展什么 - 搜索 --使用过滤器对故事和评论进行全文搜索
- 没有API密钥 -使用公共Algolia HN API
- 100%测试覆盖率 --使用VCR卡带进行测试,无需网络通话
典型的代理工作流程
1. get_thread(42123456, depth=1) → story + 85 top-level comments with reply_counts
2. Agent picks Comment A (47 replies)
3. get_comment_tree(comment_a_id) → full 47-reply subtree
4. Agent summarizes branch, picks next或者对于较小的线程,只需 get_thread(id, depth=-1) 一次得到整棵树。
入门
标准配置 适用于大多数工具:
{
"mcpServers": {
"hn": {
"command": "uvx",
"args": ["hn-mcp"]
}
}
}Claude Code
claude mcp add hn -- uvx hn-mcp添加 --scope user 使其在所有项目中都可用。
Claude Desktop
遵循MCP安装 指南,使用上面的标准配置。
Cursor
添加到光标MCP配置(~/.cursor/mcp.json):
{
"mcpServers": {
"hn": {
"command": "uvx",
"args": ["hn-mcp"]
}
}
}Windsurf
遵循Windsurf MCP 文档,使用上面的标准配置。
VS Code / Copilot
添加到您的VS代码MCP配置中(.vscode/mcp.json):
{
"mcpServers": {
"hn": {
"command": "uvx",
"args": ["hn-mcp"]
}
}
}From source
如果您想从本地克隆运行:
claude mcp add hn -- uv run --directory /absolute/path/to/news-ycombinator-mcp hn-mcp先决条件
- Python 3.12+
- 紫外线 (提供
uvx)
工具
| 工具 | 描述 | 关键输入 | 返回 |
|---|---|---|---|
get_thread | 获取一个故事及其评论树 | story_id, depth (0=仅故事,1=顶级,N=N级,-1=完整树) | 故事元数据+修剪的评论树 |
get_comment_tree | 深入了解特定评论的回复子树 | comment_id, depth (默认值:-1,完整子树) | 注释+嵌套回复 |
get_stories | 按类别浏览HN | category (top、new、ask_hn、show_hn), count | 故事摘要列表 |
search_stories | 全文搜索故事 | query, sort_by (相关性/日期), count, page | 分页的故事结果 |
search_comments | 全文搜索评论 | query, sort_by, story_id, author, count, page | 分页评论结果 |
get_user | 获取用户配置文件 | username | 用户名、因果报应、关于、创建日期 |
深度参数
这 depth 参数打开 get_thread 和 get_comment_tree 控制你得到多少树:
depth=0 (no comments — story metadata only)
depth=1 Comment A (reply_count=3) ← just the comment + count
depth=2 Comment A ← comment + direct replies
├── Reply A1 (reply_count=2)
├── Reply A2 (reply_count=0)
└── Reply A3 (reply_count=1)
depth=-1 Full tree, no pruning发展
git clone https://github.com/tomwojcik/news-ycombinator-mcp
cd news-ycombinator-mcp
make venv
make install运行测试
make test测试使用 vcrpy 卡带——无需网络通话。覆盖范围会自动报告。
重新录制磁带
如果Algolia API响应格式发生变化:
# In tests/conftest.py, temporarily change record_mode to "new_episodes"
uv run pytest
# Then change it back to "none"项目结构
src/hn_mcp/
├── app.py # FastMCP instance + tree pruning helpers
├── client.py # HNClient — async Algolia API client
├── server.py # Entrypoint
├── types.py # TypedDict definitions for all responses
└── tools/
├── get_thread.py
├── get_comment_tree.py
├── get_stories.py
├── search_stories.py
├── search_comments.py
└── get_user.py许可证
麻省理工学院——见 许可证.
贡献
欢迎捐款。请先打开一个问题,讨论您想更改的内容。
提交PR时:
- 添加新功能的测试
- 为任何新的API调用录制VCR磁带
- 确保
uv run pytest100%覆盖的通行证
