Back to MCP directory
publicPublicdnsLocal runtime

tamagotchi-mcp-server

一个模拟电子鸡养成的简易MCP服务器项目,基于Python和FastAPI实现,包含虚拟环境配置和游戏操作功能。

article

README

🚀 保姆级教程:使用 FastAPI 快速搭建属于你的 Tamagotchi MCP Server

本教程将带你使用 FastAPI 快速搭建属于自己的 Tamagotchi MCP Server,让你轻松掌握服务器搭建的核心要点,快速拥有自己的服务器。

🚀 快速开始

环境准备

确保你已经安装了 Python 环境,建议使用 Python 3.7 及以上版本。

安装依赖

在命令行中执行以下命令来安装所需的依赖:

pip install fastapi uvicorn

启动服务器

创建一个 Python 文件,例如 main.py,并编写以下代码:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

在命令行中执行以下命令启动服务器:

uvicorn main:app --reload

现在,你可以在浏览器中访问 http://127.0.0.1:8000 来查看服务器是否正常运行。

💻 使用示例

基础用法

以下是一个简单的示例,展示了如何创建一个基本的 API 端点:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

你可以通过访问 http://127.0.0.1:8000/items/5?q=somequery 来测试这个端点。

高级用法

如果你需要处理更复杂的请求,可以使用 FastAPI 的请求体和响应模型:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = None

@app.post("/items/")
def create_item(item: Item):
    return item

你可以使用工具(如 Postman)发送 POST 请求到 http://127.0.0.1:8000/items/ 并提供 JSON 数据来测试这个端点。

🔧 技术细节

FastAPI 简介

FastAPI 是一个用于构建 API 的现代、快速(高性能)的 Web 框架,它基于 Python 类型提示,使用 Starlette 和 Pydantic 来实现高性能和类型安全。

服务器选择

我们使用 Uvicorn 作为服务器,它是一个基于 ASGI(异步服务器网关接口)的服务器,支持异步编程,能够提供高效的性能。

异步编程

FastAPI 支持异步编程,你可以使用 asyncawait 关键字来编写异步函数,提高服务器的并发处理能力。

📄 许可证

本项目遵循 [具体许可证名称] 许可证。你可以在项目的根目录下找到 LICENSE 文件以获取更多详细信息。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client