GNews MCP Server
Enables searching news articles and fetching top headlines via GNews API with filtering options.
README
GNews MCP Server
A Model Context Protocol (MCP) server for fetching news articles and headlines using the GNews API.
Features
- Search News: Search for news articles using keywords with various filtering options
- Top Headlines: Get current trending headlines based on Google News ranking
- Comprehensive Filtering: Filter by language, country, date range, category, and more
- Schema Validation: Full input/output validation using JSON Schema
- Error Handling: Robust error handling and meaningful error messages
Installation
Prerequisites
- Python 3.11 or higher
- UV package manager
- GNews API key from gnews.io
Install from Git
uvx --from git+https://github.com/yourusername/gnews-mcp-server gnews-server
Local Development
-
Clone the repository:
git clone https://github.com/yourusername/gnews-mcp-server.git cd gnews-mcp-server -
Install dependencies:
uv sync -
Set up environment variables:
# Create .env file echo "GNEWS_KEY=your_gnews_api_key_here" > .env -
Run tests:
uv run python test_server.py
Configuration
Environment Variables
GNEWS_KEY(required): Your GNews API key from gnews.ioDEBUG(optional): Set totruefor debug logging
Getting a GNews API Key
- Visit gnews.io
- Sign up for a free account
- Get your API key from the dashboard
- Set it as the
GNEWS_KEYenvironment variable
Available Tools
1. search_news
Search for news articles using keywords with various filtering options.
Parameters:
query(required): Search keywords (supports logical operators AND, OR, NOT)language: 2-letter language code (default: "en") - supported values: ar, zh, nl, en, fr, de, el, hi, it, ja, ml, mr, no, pt, ro, ru, es, sv, ta, te, ukcountry: 2-letter country code - supported values: au, br, ca, cn, eg, fr, de, gr, hk, in, ie, it, jp, nl, no, pk, pe, ph, pt, ro, ru, sg, se, ch, tw, ua, gb, usin: Search in "title", "description", "content" (default: "title,description")sortby: Sort by "publishedAt" or "relevance" (default: "publishedAt")start_date: Start date in YYYY-MM-DD formatend_date: End date in YYYY-MM-DD format
Returns exactly 10 articles.
Example:
{
"query": "artificial intelligence",
"language": "en",
"country": "us",
"sortby": "relevance",
"start_date": "2024-01-01",
"end_date": "2024-01-31"
}
2. get_top_headlines
Get current trending headlines based on Google News ranking.
Parameters:
category: Category ("general", "world", "nation", "business", "technology", "entertainment", "sports", "science", "health") - default: "general"language: 2-letter language code (default: "en") - supported values: ar, zh, nl, en, fr, de, el, hi, it, ja, ml, mr, no, pt, ro, ru, es, sv, ta, te, ukcountry: 2-letter country code - supported values: au, br, ca, cn, eg, fr, de, gr, hk, in, ie, it, jp, nl, no, pk, pe, ph, pt, ro, ru, sg, se, ch, tw, ua, gb, usquery: Search keywords within headlinesstart_date: Start date in YYYY-MM-DD formatend_date: End date in YYYY-MM-DD format
Returns exactly 10 articles.
Example:
{
"category": "technology",
"language": "en",
"country": "us"
}
MCP Client Configuration
Claude Desktop
Add to your Claude Desktop configuration file:
{
"mcpServers": {
"gnews": {
"command": "uvx",
"args": [
"--from",
"git+https://github.com/yourusername/gnews-mcp-server",
"gnews-server"
],
"env": {
"GNEWS_KEY": "your_gnews_api_key_here"
}
}
}
}
Other MCP Clients
The server communicates using the standard MCP protocol over stdio. Refer to your MCP client's documentation for configuration details.
Query Syntax
The GNews API supports advanced query syntax for the query parameter:
Phrase Search
"Apple iPhone"- Exact phrase matching
Logical Operators
Apple AND iPhone- Both terms must be presentApple OR Microsoft- Either term must be presentApple NOT iPhone- First term without second term
Complex Queries
(Apple AND iPhone) OR (Google AND Pixel)- Complex logical combinations"machine learning" AND NOT "deep learning"- Phrase with exclusion
Supported Languages
| Language | Code | Language | Code |
|---|---|---|---|
| Arabic | ar | Italian | it |
| Chinese | zh | Japanese | ja |
| Dutch | nl | Norwegian | no |
| English | en | Portuguese | pt |
| French | fr | Russian | ru |
| German | de | Spanish | es |
| Greek | el | Swedish | sv |
| Hindi | hi | Ukrainian | uk |
Supported Countries
| Country | Code | Country | Code |
|---|---|---|---|
| Australia | au | Japan | jp |
| Brazil | br | Netherlands | nl |
| Canada | ca | Norway | no |
| China | cn | Pakistan | pk |
| Egypt | eg | Portugal | pt |
| France | fr | Singapore | sg |
| Germany | de | Spain | es |
| India | in | Sweden | se |
| Italy | it | United Kingdom | gb |
| United States | us | (and more...) |
Testing
The server includes comprehensive tests:
# Run all tests
uv run python test_server.py
# Test features:
# - Schema validation
# - Input/output validation
# - Error handling
# - Tool functionality
Development
Project Structure
gnews-mcp-server/
├── gnews_mcp_server/
│ ├── __init__.py
│ ├── __main__.py # Entry point
│ ├── server.py # MCP server implementation
│ ├── handlers.py # Tool implementations
│ └── tools.json # Tool schemas
├── test_cases.json # Test definitions
├── test_server.py # Test framework
├── pyproject.toml # Project configuration
└── README.md
MCP Schema Design Rules
This project follows specific patterns for MCP JSON schema design:
Optional Parameters Pattern
✅ DO - Use nullable union types for optional parameters:
{
"country": {
"type": ["string", "null"],
"description": "2-letter country code (optional)",
"enum": ["us", "gb", "ca", ...]
}
}
❌ DON'T - Use default: null with non-nullable types:
{
"country": {
"type": "string",
"default": null // Type inconsistency!
}
}
Parameters with Explicit Defaults
✅ DO - Keep meaningful defaults for parameters:
{
"language": {
"type": "string",
"enum": ["en", "fr", "de", ...],
"default": "en"
}
}
Required Array Management
- Include in
required: Only truly mandatory parameters - Exclude from
required: All optional/nullable parameters - Schema validation: Catches invalid inputs before handlers
Handler Signature Matching
Match your handler function signatures to the schema:
async def search_news(query: str, # required
language: str = "en", # has default
country: str = None, # nullable optional
**kwargs) -> dict:
Adding New Features
- Update tool schemas in
gnews_mcp_server/tools.json - Implement tool functions in
gnews_mcp_server/handlers.py - Add tests to
test_cases.json - Run tests:
uv run python test_server.py
Error Handling
The server provides comprehensive error handling:
- Validation Errors: Invalid parameters are caught and reported
- API Errors: GNews API errors are properly formatted
- Network Errors: Connection issues are handled gracefully
- Schema Validation: Input/output validation ensures data integrity
Rate Limits
GNews API rate limits depend on your subscription plan:
- Free: 100 requests per day
- Paid Plans: Higher limits available
The server will return appropriate error messages when rate limits are exceeded.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Schema Validation Benefits
This server provides enhanced schema validation features:
- Input Validation: Parameters validated against JSON Schema before processing
- Output Validation: Responses validated to ensure data integrity
- Type Safety: Nullable types properly handled with union types
["string", "null"] - Error Prevention: Invalid enum values, date formats, and required fields caught early
- Consistent Behavior: Predictable parameter handling across all tools
Changelog
v0.1.0
- Initial release
- Search news functionality
- Top headlines functionality
- Comprehensive schema validation with nullable union types
- Enhanced error handling and input validation
- Full test suite with 100% coverage
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。