当前位置: 首页 > news >正文

在 OCI 生成式 AI 上搭一个「指定地区拉面店 MCP Server」——从 0 到 1 实战记录

在 OCI 生成式 AI 上搭一个「指定地区拉面店 MCP Server」——从 0 到 1 实战记录

    • 1. 项目缘起
    • 2. 运行环境
    • 3. 15 分钟极速搭环境
      • 3.1 安装 uv 并初始化项目
      • 3.2 依赖一次到位
    • 4. MCP Server 代码(ramen.py)
    • 5. MCP Client 代码(client.py)
    • 6. 跑起来!
      • 典型输出节选
    • 7. 小结

参考文章:OCI生成AIサービスで指定した地域のラーメン屋情報を教えてくれるMCPサーバを作ってみた


1. 项目缘起

顺手把最近很火的 MCP(Model Context Protocol) 拿来练手,做了一个「你报地名、我给拉面店」的小服务,跑在 Oracle Cloud Infrastructure(OCI)最新发布的 cohere.command-a-03-2025 大模型上。

一句话需求:
用户说「帮我找つくば的拉面店」,Agent 自动返回店铺列表。


2. 运行环境

组件说明
计算OCI Compute – Oracle Linux 8(1 OCPU / 16 GB)
Python3.11(uv 管理)
LLMcohere.command-a-03-2025(东京/大阪区域均可)
数据ホットペッパーグルメ Web サービス API

3. 15 分钟极速搭环境

3.1 安装 uv 并初始化项目

curl -LsSf https://astral.sh/uv/install.sh | sh
uv init ramen && cd ramen
uv venv && source .venv/bin/activate

3.2 依赖一次到位

uv add "mcp[cli]" httpx langchain_community langchain_mcp_adapters langgraph oci

还需要本地安装/配置 oci-cli。


4. MCP Server 代码(ramen.py)

核心思路:

  1. 根据用户给出的城市名 → 查 小エリアコード
  2. 拿小エリアコード → 查拉面店列表
import os, httpx
from dotenv import load_dotenv
from typing import Annotated
from mcp.server.fastmcp import FastMCPload_dotenv()
API_KEY = os.environ["API_KEY"]
API_BASE = "http://webservice.recruit.co.jp/hotpepper"
USER_AGENT = "ramen-app/1.0"mcp = FastMCP("ramen")@mcp.tool()
async def get_small_area_code(area_name: Annotated[str, "市区町村名 例: 京都市"]
) -> str:url = f"{API_BASE}/small_area/v1/?key={API_KEY}&format=json"async with httpx.AsyncClient() as c:r = await c.get(url, headers={"User-Agent": USER_AGENT}, timeout=30)r.raise_for_status()for a in r.json()["results"]["small_area"]:if area_name in a["name"]:return a["code"]return ""@mcp.tool()
async def get_gourmet_info(small_area_code: Annotated[str, "小エリアコード 例: X587"]
) -> str:url = (f"{API_BASE}/gourmet/v1/?key={API_KEY}&small_area={small_area_code}"f"&genre=G013&format=json")async with httpx.AsyncClient() as c:r = await c.get(url, headers={"User-Agent": USER_AGENT}, timeout=30)r.raise_for_status()shops = "\n".join([f"店名: {s['name']} {s['name_kana']}\n特徴: {s['catch']}\n"for s in r.json()["results"]["shop"]])return shopsif __name__ == "__main__":mcp.run(transport="stdio")

5. MCP Client 代码(client.py)

LangGraph ReAct Agent 自动调用上面两个工具。

import asyncio, oci
from langchain_community.chat_models import ChatOCIGenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agentconfig = oci.config.from_file("~/.oci/config", "DEFAULT")
llm = ChatOCIGenAI(model_id="cohere.command-a-03-2025",service_endpoint="https://inference.generativeai.ap-osaka-1.oci.oraclecloud.com",compartment_id="your-compartment-id",model_kwargs={"temperature": 0.7, "max_tokens": 500}
)async def main():async with MultiServerMCPClient({"ramen": {"command": "python","args": ["/home/opc/ramen/ramen.py"],"transport": "stdio",}}) as client:tools = await client.get_tools()agent = create_react_agent(llm, tools)ans = await agent.ainvoke({"messages": "つくばのラーメン屋を教えて下さい。"})for m in ans["messages"]:print(f"\n--- {type(m).__name__} ---\n{m.content}")if __name__ == "__main__":asyncio.run(main())

6. 跑起来!

$ uv run client.py

典型输出节选

--- HumanMessage ---
content='つくばのラーメン屋を教えて下さい。'--- AIMessage ---
tool_calls=[{'name': 'get_small_area_code', 'args': {'area_name': 'つくば'}, ...}]--- ToolMessage ---
content='X587'--- AIMessage ---
tool_calls=[{'name': 'get_gourmet_info', 'args': {'small_area_code': 'X587'}, ...}]--- AIMessage ---
content='以下のラーメン屋がつくばにあります。
- 山岡家 つくば中央店
- 天下一品 つくば店
- 丸源ラーメン つくば店
- 龍郎
- 活龍'

7. 小结

  • MCP Server 不到 100 行即可完成,可插拔 地接入任何兼容 MCP 的 Agent 框架。
http://www.dtcms.com/a/277879.html

相关文章:

  • 《数据库》MySQL事务
  • gcc 源码阅读--C语言预处理
  • (一)SAP Group Reporting (GR) 集团财务合并解决方案套件概述
  • 构造函数延伸应用
  • [Python 基础课程]字典
  • 代码随想录算法训练营第十七天
  • spring--@Autowired
  • LlamaIndex Querying 自定义查询
  • JavaScript数据结构算法
  • js入门01
  • YOLOv5目标检测标准化流程
  • 013_流式输出与实时响应
  • 【SSM】SpringBoot 实现邮件发送
  • Typecho博客新文章自动添加“New“标签的实现方案
  • 热点代码探测确定何时JITTest01
  • 16. JVM调优工具
  • 华为OD 处理器
  • 格密码--LWE,DLWE和ss-LWE
  • 【王树森推荐系统】重排04:DPP 多样性算法(上)
  • python学习打卡:DAY 45 Tensorboard使用介绍
  • 言语理解高频词之生僻成语
  • 驱动开发(3)|rk356x驱动GPIO基础应用之点亮led灯
  • idea docker插件连接docker失败
  • [RPA] 批量数据抓取指定商品名称信息
  • Pandas-数据清洗与处理
  • Spring高级特性——反射和动态代理的性能优化
  • SQL预编译:安全高效数据库操作的关键
  • 《1.5倍与2倍的扩容密码:Java容器的内存性能抉择》
  • 【牛客刷题】四个选项:高考选择题方案统计(并查集+动态规划)
  • 01.深入理解 Python 中的 if __name__ == “__main__“