Back to MCP directory
publicPublicdnsLocal runtime

watsonx-mcp-server

本项目是一个基于IBM Watsonx.ai和MCP协议的Python聊天机器人服务器,提供医疗咨询功能,支持通过Flask构建Web界面,实现症状诊断和个性化问候。

article

README

🚀 使用Watsonx.ai构建全栈聊天机器人服务器的完整指南

本项目展示了如何使用Watsonx.ai构建一个全栈聊天机器人,并借助MCP(机器学习平台)进行管理。该方案具备清晰的分层架构,可实现对话状态存储、支持多轮对话,还能与Watsonx.ai的强大LLM(大语言模型)集成。

🚀 快速开始

读者可通过运行server.pychatbot.py脚本,结合HTML模板文件来体验完整的聊天机器人功能。

✨ 主要特性

  • 采用清晰的分层架构,便于维护、扩展和优化。
  • 实现对话状态存储,支持多轮对话。
  • 与Watsonx.ai的强大LLM集成。

📦 安装指南

文档中未提及具体安装步骤,可参考以下最佳实践进行环境准备:

  1. 使用虚拟环境管理依赖。
  2. 配置.env文件来存储敏感信息。
  3. 启用错误日志记录以便调试。
  4. 定期更新模型版本以获得最佳性能。

💻 使用示例

基础用法

运行以下脚本启动项目:

# 运行主服务器入口,启动Flask应用
# server.py
from flask import Flask, request, render_template, redirect, url_for
import os

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/symptoms', methods=['POST'])
def symptoms():
    name = request.form.get('name')
    return render_template('symptoms.html', greeting=f'Hello {name}!')

@app.route('/diagnosis', methods=['POST'])
def diagnosis():
    symptoms = request.form.get('symptoms')
    # 调用MCP服务获取诊断结果
    result = call_watsonx(symptoms)
    return render_template('diagnosis.html', diagnosis=result)

if __name__ == '__main__':
    app.run(debug=True, port=5000)

# 初始化MCP客户端,负责调用Watsonx.ai服务
# chatbot.py
import os
from mcp.clients import MCPClient

# 初始化MCP客户端
mcp = MCPClient(
    endpoint=os.getenv('MCP_ENDPOINT'), 
    api_key=os.getenv('MCP_API_KEY'),
    verbose=True  # 启用调试模式输出
)

def call_watsonx(symptoms):
    # 使用预定义的WatsonX模型进行诊断
    response = mcp.chat(
        model='watsonx-33b-instruct', 
        messages=[{
            'role': 'user',
            'content': f'Based on the symptoms {symptoms}, provide a medical diagnosis and recommendations.'
        }]
    )
    return response.choices[0].message.content

高级用法

可参考以下扩展建议对项目进行扩展:

  1. 实现会话状态存储(使用Redis)。
  2. 添加用户认证功能。
  3. 集成第三方医疗知识库。
  4. 优化UI界面增加更多交互功能。

📚 详细文档

项目结构

project/
├── server.py           # 主服务器入口,启动Flask应用
├── chatbot.py         # MCP客户端,负责调用Watsonx.ai服务
├── templates/         # HTML模板文件夹,包含三个页面的模版
│   ├── index.html     # 首页,收集用户姓名
│   ├── symptoms.html  # 症状输入页面
│   └── diagnosis.html # 诊断结果展示页面
└── assets/            # 存放截图和其他资源文件

HTML模板实现

首页(index.html)

<!DOCTYPE html>
<html>
<head>
    <title>Watsonx Chatbot</title>
</head>
<body>
    <h1>Welcome to Watsonx Chatbot</h1>
    <form method="post">
        <label>Your Name:</label><br>
        <input type="text" name="name"><br>
        <button type="submit">Start Chatting</button>
    </form>
</body>
</html>

症状页面(symptoms.html)

<!DOCTYPE html>
<html>
<head>
    <title>Report Symptoms</title>
</head>
<body>
    <h1>{{ greeting }}</h1>
    <h2>Please describe your symptoms:</h2>
    <form method="post">
        <textarea name="symptoms" rows="4"></textarea><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

诊断页面(diagnosis.html)

<!DOCTYPE html>
<html>
<head>
    <title>Diagnosis Result</title>
</head>
<body>
    <h1>Your Medical Diagnosis:</h1>
    <pre>{{ diagnosis }}</pre>
</body>
</html>

🔧 技术细节

本项目通过Flask提供Web界面,并与Watsonx.ai的LLM服务无缝集成。采用分层架构,server.py作为主服务器入口启动Flask应用,chatbot.py作为MCP客户端负责调用Watsonx.ai服务,HTML模板文件提供用户交互界面。

📄 故障排除

常见问题及解决方案

| 属性 | 详情 | |------|------| | 无法连接到MCP服务 | 检查环境变量是否正确配置,确保API密钥有效 | | 诊断结果为空 | 确保症状描述完整且清晰 | | 页面加载失败 | 检查服务器是否正常运行在5000端口 |

📄 结论

本项目展示了如何构建一个完整的聊天机器人系统,通过Flask提供Web界面,并与Watsonx.ai的LLM服务无缝集成。这种分层架构不仅易于维护,还便于扩展和优化。

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