main.py•944 B
from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Initialize FastMCP server
mcp = FastMCP("mcp-blog")
# Get the mock API URL from environment variables
mock_api_url = os.getenv("MOCK_API_URL")
@mcp.tool()
def get_blogs():
"""Fetch all blogs from the mock API."""
response = httpx.get(mock_api_url)
return response.json()
@mcp.tool()
def search_blogs(query: str):
"""Search for blogs by title using the mock API."""
response = httpx.get(mock_api_url, params={'title': query})
return response.json()
@mcp.tool()
def add_blog(title: str, body: str):
"""Add a new blog to the mock API."""
response = httpx.post(mock_api_url, json={'title': title, 'body': body})
return response.json()
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')