News Aggregator API

News Aggregator API

A modular, scalable news aggregation backend that provides a unified interface to TheNewsAPI for retrieving current and historical news articles with advanced filtering capabilities, designed specifically for consumption by AI agents.

Category
访问服务器

README

News Aggregator API (MCP Server)

A modular, scalable news aggregation backend built with MCP server conventions and TypeScript. This API provides a unified interface to TheNewsAPI for retrieving current and historical news articles with advanced filtering capabilities. Designed specifically for consumption by AI agents, this API prioritizes structured data and consistent patterns.

Features

  • Unified API: Consistent interface for accessing news data
  • TypeScript: Fully typed for reliable development
  • MCP Architecture: Scalable, modular design with separation of concerns
  • Multiple Endpoints: Access various types of news content
  • Environment Variables: Secure configuration management
  • Error Handling: Consistent error formats with appropriate status codes
  • Filtering: Support for various filtering parameters
  • Caching: Intelligent caching system with management endpoints
  • Interactive Documentation: Swagger-based API documentation
  • AI-Friendly Responses: Structured data optimized for machine consumption

Endpoints

News Endpoints

Endpoint Description Example
/api/news/top Get top news headlines /api/news/top?categories=business
/api/news/all Get all news with advanced search /api/news/all?search=technology
/api/news/similar/:uuid Get similar articles to a specific one /api/news/similar/cc11e3ab-ced0-4a42-9146-e426505e2e67
/api/news/uuid/:uuid Get a specific article by UUID /api/news/uuid/cc11e3ab-ced0-4a42-9146-e426505e2e67
/api/news/sources Get available news sources /api/news/sources?language=en

Cache Management Endpoints

Endpoint Description Example
/api/cache/stats Get cache statistics /api/cache/stats
/api/cache/clear Clear all cache DELETE /api/cache/clear
/api/cache/clear/:type Clear cache by type DELETE /api/cache/clear/top

Utility Endpoints

Endpoint Description
/health Health check endpoint
/docs Interactive API documentation
/docs.json OpenAPI specification
/examples Usage examples

Testing

This project implements a comprehensive testing strategy with multiple layers of tests to ensure reliability and correctness.

Test Types

  1. Unit Tests

    • Isolated tests for individual components
    • Mocked dependencies for true unit isolation
    • Focused on business logic validation
  2. Controller Tests

    • Verify controller behavior with mocked services
    • Test proper request handling and response formatting
    • Confirm error handling patterns are consistent
  3. Integration Tests

    • Test the complete request-response cycle
    • Verify API endpoints with supertest
    • Mock external API calls for reliability

Testing Best Practices

This project follows these testing best practices:

  • Isolation - Tests don't depend on each other or external services
  • Repeatable - Tests produce the same results on any environment
  • Fast - Mocking external dependencies keeps tests efficient
  • Comprehensive - Core functionality has high test coverage
  • Maintainable - Tests follow consistent patterns and structure

Running Tests

# Run all tests
npm test

# Run tests with coverage
npm run test:coverage

# Run a specific test file
npx jest path/to/test.ts

Installation

# Clone the repository
git clone <repository-url>

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env file with your own API key and database URL

# Generate Prisma client
npx prisma generate

# Build the project
npm run build

# Start the server
npm start

Configuration

Create a .env file with the following variables:

# News Aggregator API Environment Variables

# API Configuration
NEWS_API_TOKEN=your_api_token_here

# Server Configuration
PORT=3000
NODE_ENV=development

# Database Configuration
DATABASE_URL="file:./dev.db"

Database Configuration

The application uses Prisma ORM with SQLite by default. The DATABASE_URL environment variable should point to your database file. For production, you can configure it to use PostgreSQL or MySQL.

Development

# Run in development mode with hot reload
npm run dev

# Run linting
npm run lint

# Run tests
npm test

Testing Infrastructure

The project includes a comprehensive test suite built with Jest and Supertest, covering API endpoints, database integration, and server connectivity checks.

Running Tests

# Run all tests
npm test

# Run only API endpoint tests
npm run test:api

# Run only database integration tests
npm run test:db

# Run tests with coverage report
npm run test:coverage

# Run tests in watch mode (for development)
npm run test:watch

# Check server connectivity
npm run check-server

Test Structure

  • API Endpoint Tests: Located in src/__tests__/api/, these tests verify that all API endpoints return the expected responses, handle errors correctly, and apply appropriate filtering.

  • Database Integration Tests: Located in src/__tests__/database.test.ts, these tests verify database connectivity, query execution, and transaction support.

  • Server Connectivity Script: Located in scripts/check-server.ts, this script tests connectivity to a running server instance, verifying that critical endpoints are functioning correctly.

Test Environment

Tests use a separate environment configuration specified by setting NODE_ENV=test. This ensures tests don't interfere with development or production environments. The setup includes:

  • Automatic database connection setup and teardown
  • Cache clearing between tests to ensure isolation
  • Server startup and shutdown for endpoint testing

Future Enhancements Todo List

Priority 1: Foundation Improvements

  • [x] Caching System

    • Implement in-memory caching
    • Cache frequently accessed data like top news and sources
    • Add cache invalidation strategies
    • Configure TTL (Time-To-Live) for different content types
    • Add cache management endpoints
  • [x] Data Persistence Layer

    • Add database integration (SQLite with Prisma)
    • Create schema for storing enhanced article metadata
    • Implement repository pattern for data access
    • Add migration system for schema changes
  • [x] API Documentation

    • Generate OpenAPI/Swagger specification
    • Add interactive API documentation
    • Create usage examples for each endpoint

API Documentation

The API now includes comprehensive documentation using OpenAPI/Swagger:

  • Interactive API Documentation: Available at /docs when the server is running
  • OpenAPI Specification: JSON format available at /docs.json
  • Usage Examples: Available at /examples with code samples in multiple languages

How to Access the Documentation

  1. Start the server with npm run dev or npm start
  2. Open your browser to http://localhost:3000/docs for the interactive Swagger UI
  3. Browse through the available endpoints, request parameters, and response formats
  4. Reference implementation examples at http://localhost:3000/examples

API Documentation Features

  • Complete endpoint descriptions with parameters and response types
  • Interactive "Try it out" capability to test endpoints directly
  • Request/response schema definitions
  • Code snippets for API consumption
  • Examples of integrating with the MCP server architecture

Priority 2: Security & Performance

  • [ ] Authentication & User Management

    • Implement JWT-based authentication
    • Create user registration and login endpoints
    • Add role-based access control
    • Implement secure password handling
  • [ ] Rate Limiting

    • Add request throttling middleware
    • Implement tiered access levels
    • Create fair usage policies
    • Add rate limit headers in responses
  • [ ] Monitoring & Analytics

    • Set up request logging and monitoring
    • Add performance metrics collection
    • Create usage dashboards
    • Implement error tracking and alerting

Priority 3: Enhanced Functionality

  • [ ] Content Processing

    • Add text summarization capabilities
    • Implement entity extraction
    • Add sentiment analysis
    • Create topic categorization beyond source categories
    • Implement duplicate detection and grouping
  • [ ] Personalization

    • Add user preference tracking
    • Create personalized endpoints based on reading history
    • Implement topic following functionality
    • Add recommendation engine
  • [ ] Webhooks & Real-time Updates

    • Implement webhook registration
    • Create event-based notification system
    • Add SSE (Server-Sent Events) for real-time updates
    • Implement topic subscription functionality

Contributing

Contributions to the News Aggregator API project are welcome! This project is designed to be a friendly, well-documented codebase for those interested in MCP server architecture, especially with AI consumption in mind.

How to Contribute

  1. Fork the repository
  2. Create a new feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests to ensure everything works (npm test)
  5. Commit your changes (git commit -m 'Add some amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Code Style

Please follow the existing code style and patterns in the project. This project uses ESLint for code quality and formatting.

Security Considerations

When contributing, please ensure:

  • No API keys or credentials are hardcoded
  • Environment variables are used for configuration
  • No sensitive data is logged
  • Input validation is properly implemented

Testing

All new features should include appropriate tests. Please follow the existing testing patterns in the project.

  • [ ] Search History & Bookmarks
    • Add saved searches functionality
    • Implement article bookmarking
    • Create reading history tracking
    • Add user collections feature

Contributing

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature/my-new-feature
  5. Submit a pull request

License

ISC

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选