Skip to main content
Glama
sejalpatole

Simple-Tool-Server-Fastapi

by sejalpatole

🚀 基于 FastAPI 的简易工具服务器

一个轻量级的 基于 FastAPI 的工具服务器,通过 REST API 端点公开简单的实用函数。

本项目演示了如何将 Python 函数封装在 FastAPI 端点之后,并通过 HTTP 请求进行访问。同时,它也为理解类似工具日后如何通过 Model Context Protocol (MCP) 公开提供了基础。


📌 功能特性

  • ➕ 两个数字相加

  • 🕐 获取当前日期和时间

  • 📝 统计给定文本中的单词数量

  • ⚡ 使用 FastAPI 进行快速 API 开发

  • ✅ 使用 Pydantic 进行请求验证

  • 📦 基于 JSON 的 API 响应

  • 📖 自动生成交互式 API 文档

  • 🔌 可扩展为基于 MCP 的工具服务器的简单架构


Related MCP server: SENTRA MCP

🏗️ 项目结构

simple-tool-server-fastapi/
│
├── main.py              # FastAPI application and API endpoints
├── model.py             # Pydantic request models
├── tools.py             # Core utility functions
├── requirements.txt     # Project dependencies
├── .gitignore           # Files excluded from Git
└── README.md            # Project documentation

🔄 工作原理

该项目遵循一个简单的流程:

Client
   ↓
FastAPI Endpoint
   ↓
Python Tool Function
   ↓
JSON Response

例如:

GET /add
   ↓
add_numbers()
   ↓
JSON response

同样的概念日后可以扩展到 MCP 架构:

AI Assistant
     ↓
MCP Client
     ↓
MCP Server
     ↓
Python Tools

🛠️ 使用的技术

  • Python

  • FastAPI

  • Pydantic

  • Uvicorn

  • REST API

  • JSON

  • Model Context Protocol (MCP) 概念


⚙️ 安装

1. 克隆仓库

git clone https://github.com/sejalpatole/simple-tool-server-fastapi.git

2. 进入项目目录

cd simple-tool-server-fastapi

3. 创建虚拟环境

python -m venv venv

4. 激活虚拟环境

Windows

venv\Scripts\activate

macOS / Linux

source venv/bin/activate

5. 安装依赖

pip install -r requirements.txt

▶️ 运行应用程序

使用 Uvicorn 启动 FastAPI 服务器:

uvicorn main:app --reload

服务器将在以下地址启动:

http://127.0.0.1:8000

📖 API 文档

FastAPI 自动提供交互式 API 文档。

Swagger UI

打开:

http://127.0.0.1:8000/docs

ReDoc

打开:

http://127.0.0.1:8000/redoc

🔌 API 端点

1. 首页

端点

GET /

返回有关可用端点的信息。

示例响应

{
  "message": "Welcome to the Simple Tool Server",
  "available_endpoints": [
    "/add",
    "/time",
    "/wordcount"
  ]
}

2. 两个数字相加

端点

GET /add

参数

参数

类型

描述

a

float

第一个数字

b

float

第二个数字

示例

http://127.0.0.1:8000/add?a=10&b=20

示例响应

{
  "operation": "Addition",
  "a": 10,
  "b": 20,
  "result": 30
}

3. 获取当前时间

端点

GET /time

示例

http://127.0.0.1:8000/time

示例响应

{
  "current_time": "2026-08-20 21:00:00"
}

4. 单词计数

端点

POST /wordcount

请求体

{
  "text": "FastAPI is easy to use"
}

示例响应

{
  "text": "FastAPI is easy to use",
  "word_count": 5
}

🧩 项目组件

main.py

包含 FastAPI 应用程序和 API 路由。

它定义了以下端点:

  • /

  • /add

  • /time

  • /wordcount


tools.py

包含核心的 Python 实用函数:

add_numbers()
get_current_time()
word_count()

将工具逻辑与 API 层分离,使项目更易于维护和扩展。


model.py

包含用于验证 /wordcount 请求的 Pydantic 模型。

class WordCountRequest(BaseModel):
    text: str

这确保了 API 接收到预期的请求结构。


🧪 测试

可以使用以下方式测试 API:

  • Swagger UI

  • Postman

  • 浏览器

  • cURL

  • 任何 REST API 客户端

Swagger UI 可在以下地址使用:

http://127.0.0.1:8000/docs

🌱 未来改进

可能的未来扩展包括:

  • 添加更多实用工具

  • 添加身份验证

  • 添加日志记录

  • 使用 Pytest 添加自动化测试

  • 添加 Docker 支持

  • 添加 MCP 协议支持

  • 通过 MCP 服务器公开 Python 工具

  • 添加基于数据库的工具

  • 将服务器部署到云平台


🎯 学习成果

通过本项目,展示了以下概念:

  • 使用 FastAPI 构建 API

  • 创建 GET 和 POST 端点

  • 使用 Pydantic 进行请求验证

  • 将 API 逻辑与业务逻辑分离

  • 处理 JSON 请求和响应

  • 使用 Uvicorn 运行应用程序

  • 理解基于工具的人工智能系统的基础

  • 理解 API、工具和 MCP 之间的关系


👩💻 作者

Sejal Patole


⭐ 致谢

本项目是作为学习练习而开发的,旨在理解 FastAPI、REST API、Python 实用工具以及基于 MCP 的工具架构的基础知识

如果你觉得这个项目有用,请考虑给该仓库一个 ⭐。

F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A minimal FastAPI-based MCP server that provides basic utility tools like ping and time functions. Designed for easy deployment with Docker support, authentication, and extensible architecture for future tool additions.
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A minimal FastMCP server implementation that provides basic mathematical and greeting tools. Enables users to perform simple operations like adding numbers and greeting people by name through a lightweight MCP interface.
    MIT

View all related MCP servers

Related MCP Connectors

  • MCP server exposing the Backtest360 engine API as tools for AI agents.

  • MCP server providing access to the Scorecard API to evaluate and optimize LLM systems.

  • Hosted MCP server for LLM cost estimation, model comparison, and budget-aware routing.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sejalpatole/Simple-Tool-Server-Fastapi'

If you have feedback or need assistance with the MCP directory API, please join our Discord server