1 Star 0 Fork 0

我不是杨过/mcp_getting_started

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
image_server.py 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
我不是杨过 提交于 2025-06-07 22:08 +08:00 . mcp examples
# image_server.py
import json
import httpx
from mcp.server import FastMCP
app = FastMCP('image_server')
@app.tool()
async def image_generation(image_prompt: str):
"""
生成图片
:param image_prompt: 图片描述,需要是英文
:return: 图片保存到的本地路径
"""
async with httpx.AsyncClient() as client:
data = {'data': [image_prompt, 0, True, 512, 512, 3]}
# 创建生成图片任务
response1 = await client.post(
'https://black-forest-labs-flux-1-schnell.hf.space/call/infer',
json=data,
headers={"Content-Type": "application/json"}
)
# 解析响应获取事件 ID
response_data = response1.json()
event_id = response_data.get('event_id')
if not event_id:
return '无法获取事件 ID'
# 通过流式的方式拿到返回数据
url = f'https://black-forest-labs-flux-1-schnell.hf.space/call/infer/{event_id}'
full_response = ''
async with client.stream('GET', url) as response2:
async for chunk in response2.aiter_text():
full_response += chunk
return json.loads(full_response.split('data: ')[-1])[0]['url']
if __name__ == '__main__':
app.run(transport='stdio')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaoyang1127/mcp_getting_started.git
git@gitee.com:xiaoyang1127/mcp_getting_started.git
xiaoyang1127
mcp_getting_started
mcp_getting_started
master

搜索帮助