Skip to content

differentstuff/mcp-news-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP News Search Wrapper

A Node.js MCP (Model Context Protocol) server wrapper that enables AI systems to interactively query news sources through an existing Python news search tool.

Overview

This wrapper exposes the Python CLI News Search tool as an MCP server endpoint, allowing AI assistants to search across 1200+ news sources worldwide using the standard MCP protocol.

Environment Variables Configuration

Required Environment Variables

Variable Description Example Default
NEWS_SEARCH_PYTHON_PATH Absolute path to the Python script /home/user/travis-news-search/cli.py ❌ Required
PYTHON_EXECUTABLE Python executable command python3 or python python3

Optional Environment Variables

Variable Description Example Default
NEWS_SEARCH_TIMEOUT Search timeout in milliseconds 30000 30000
NEWS_SEARCH_MAX_PROCESSES Maximum concurrent Python processes 5 3
MCP_SERVER_PORT MCP server port (if using HTTP transport) 3001 No default (uses stdio)
LOG_LEVEL Logging verbosity debug, info, warn, error info

Configure Environment Variables

Windows (Command Prompt)

set NEWS_SEARCH_PYTHON_PATH="C:\path\to\travis-news-search\cli.py"
set PYTHON_EXECUTABLE="python"
set NEWS_SEARCH_TIMEOUT=30000

Windows (PowerShell)

$env:NEWS_SEARCH_PYTHON_PATH="C:\path\to\travis-news-search\cli.py"
$env:PYTHON_EXECUTABLE="python"
$env:NEWS_SEARCH_TIMEOUT=30000

Linux/Mac

export NEWS_SEARCH_PYTHON_PATH="/home/user/travis-news-search/cli.py"
export PYTHON_EXECUTABLE="python3"
export NEWS_SEARCH_TIMEOUT=30000

4. Run the MCP Server

node src/index.js

Usage

Configuration with Claude Desktop

Add this to your Claude Desktop configuration file:

Windows: %APPDATA%\Claude\claude_desktop_config.json Mac: ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "news-search": {
      "command": "node",
      "args": ["C:\\MPC_Server_Share\\mcp-news-wrapper\\src\\index.js"],
      "env": {
        "NEWS_SEARCH_PYTHON_PATH": "C:\\path\\to\\travis-news-search\\cli.py",
        "PYTHON_EXECUTABLE": "python",
        "NEWS_SEARCH_TIMEOUT": "30000"
      }
    }
  }
}

Tool Usage in AI Chat

Once configured, AI assistants can use the tool like this:

User: Search for news about artificial intelligence
AI: I'll search for news about artificial intelligence.

[AI internally calls: news_search(query="artificial intelligence")]

How It Works

Architecture

AI Assistant → MCP Protocol → Node.js Wrapper → Python CLI → News APIs

Process Flow

  1. AI Request: AI assistant calls the news_search tool via MCP
  2. Node.js Wrapper: Receives MCP request, validates parameters
  3. Python Process: Spawns child process executing the Python script
  4. News Search: Python script queries news APIs/sources
  5. Response Processing: Parses JSON output, formats for MCP
  6. Return Results: Sends structured data back to AI assistant

Data Flow

  • Input: Search query string, optional maxResults count
  • Output: Structured JSON with news articles (title, content, source, URL, dates)
  • Error Handling: Graceful error messages with detailed logging

API Reference

MCP Tool: news_search

Description: Searches news across 1200 sources worldwide. Use clear and unique search terms. The output is a JSON string containing titles, descriptions, content, authors, dates, and URLs.

Input Schema:

{
  "type": "object",
  "properties": {
    "query": {
      "type": "string",
      "description": "Search query for news articles"
    },
    "maxResults": {
      "type": "number",
      "description": "Maximum number of results to return"
    }
  },
  "required": ["query"]
}

Output Schema:

{
  "success": true,
  "query": "artificial intelligence",
  "count": 10,
  "results": [
    {
      "title": "AI Breakthrough in Medical Diagnosis",
      "content": "Recent advances in artificial intelligence...",
      "source": "Tech News Daily",
      "url": "https://example.com/ai-breakthrough",
      "publishedAt": "2024-01-15T10:30:00Z",
      "author": "John Doe"
    }
  ]
}

Requirements

System Requirements

  • Node.js: 14.0 or higher
  • Python: 3.8 or higher
  • Python Script: Existing CLI news search tool at the specified path

Python Script Requirements

The Python script must accept command-line arguments:

  • Positionitional or --query parameter for search terms
  • Optional --max-results parameter for result limiting
  • Output structured JSON data to stdout
  • Return exit code 0 for success, non-zero for errors

Dependencies

The Node.js wrapper requires:

  • @modelcontextprotocol/sdk: MCP protocol implementation
  • Node.js built-in modules: child_process, path, fs

Troubleshooting

Common Issues

1. "Python script not found at path"

Environment Variable: NEWS_SEARCH_PYTHON_PATH Solution: Ensure the path is absolute and the file exists

# Test the path
ls -la "$NEWS_SEARCH_PYTHON_PATH"

2. "Python executable not found"

Environment Variable: PYTHON_EXECUTABLE Solution: Set to correct Python command (python, python3, full path)

3. "Permission denied" errors

Solutions:

  • Make Python script executable: chmod +x cli.py
  • Check file permissions: ls -la cli.py
  • Run as user with appropriate permissions

4. "Process timeout"

Environment Variable: NEWS_SEARCH_TIMEOUT Solution: Increase timeout value (default: 30 seconds)

5. "JSON parse error"

Cause: Python script output is not valid JSON Solutions:

  • Test Python script directly: python3 cli.py --query "test"
  • Check for Python runtime errors in stderr
  • Verify script outputs clean JSON to stdout

Debugging

Enable Debug Logging

export LOG_LEVEL=debug
node src/index.js

Test Python Script Manually

"$PYTHON_EXECUTABLE" "$NEWS_SEARCH_PYTHON_PATH" --query "test search" --max-results 5

Check Environment Variables

# Linux/Mac
env | grep NEWS_SEARCH

# Windows
echo %NEWS_SEARCH_PYTHON_PATH%
echo %PYTHON_EXECUTABLE%

Performance Considerations

Optimization Tips

  • Timeouts: Adjust NEWS_SEARCH_TIMEOUT based on typical search duration
  • Concurrent Processes: Use NEWS_SEARCH_MAX_PROCESSES to limit resource usage
  • Python Cold Start: Consider process pooling for frequent use

Resource Usage

  • Memory: ~50MB base + Python process overhead
  • CPU: Minimal during idle, spike during searches
  • Network: Dependent on news API usage by Python script

Security Considerations

Input Validation

  • Search queries are sanitized but passed to Python script
  • File paths are validated before execution
  • Process timeouts prevent hanging

Environment Security

  • Keep Python script path in secure directory
  • Use absolute paths to prevent path traversal
  • Set appropriate file permissions

Network Security

  • All network requests go through Python script
  • Configure SSL certificates if needed for news APIs
  • Consider rate limiting for high-usage scenarios

Development

Project Structure

mcp-news-wrapper/
├── package.json              # Node.js dependencies and scripts
├── README.md                 # This documentation
└── src/
    ├── config.js             # Environment variable configuration
    ├── index.js              # Main MCP server entry point
    ├── python-wrapper.js     # Python process management
    └── tools/
        └── news-search.js    # MCP tool definition

Adding New Features

  • MCP tools go in src/tools/
  • Configuration changes in src/config.js
  • Core server logic in src/index.js

Testing

# Manually test the python wrapper
node -e "
const wrapper = require('./src/python-wrapper.js');
const PythonWrapper = require('./src/python-wrapper.js');
const w = new PythonWrapper();
w.searchNews('test', 5).then(console.log).catch(console.error);
"

License

This wrapper is designed to work with existing Python news search tools. Ensure you have proper licenses for any news APIs or content accessed through the underlying Python script.

Support

For issues with:

  • Python Script: Contact the original script author
  • MCP Protocol: Check Model Context Protocol documentation
  • Node.js Wrapper: Verify environment variables and Python script compatibility

About

npx wrapper to use travis as mcp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published