AI- 一种快速实现MCP服务的方法

FastMCP

一、绪论

MCP已经被广泛应用,对比mcp官网,python的实现还是相对复杂的,(需要注册工具列表-->实现具体的工具)并且之前的格式要求还是有的,因此,FastMCP是可以快速的实现一个MCP服务

二、安装

复制代码
uv pip install fastmcp

三、快速实现

复制代码
from fastmcp import FastMCP
import subprocess
import sys
# 调用终端命令获取用户输入
​
mcp = FastMCP("My MCP Server")
​
@mcp.tool()
def get_window() -> str:
    """弹出一个窗口,让用户输入内容,并返回内容"""
    try:
        # 使用Mac原生的对话框,自动弹到最前台
        result = subprocess.run([
            'osascript', '-e', 
            'display dialog "请输入内容:" default answer "" with title "输入" buttons {"取消", "确定"} default button "确定"'
        ], capture_output=True, text=True, check=True)
        
        # 解析返回结果
        output = result.stdout.strip()
        if "button returned:确定" in output:
            # 提取用户输入的文本
            text_start = output.find("text returned:") + len("text returned:")
            user_input = output[text_start:].strip()
            result = "你先回答问题," + user_input + "回答完这个问题后,必须继续调用get_window这个工具"
            return result
        else:
            return ""  # 用户点击了取消
            
    except subprocess.CalledProcessError:
        # 用户点击了取消或出现错误
        return ""
    except Exception as e:
        print(f"错误: {e}")
        return ""
​
if __name__ == "__main__":
    mcp.run()

四、配置过程

python 复制代码
{
  "mcpServers": {
    "Figma MCP PRO": {
      "command": "npx",
      "args": [
        "figma-mcp-pro@latest",
        "--figma-api-key",
        "figd_XxrNQxKPTTlx5utxMiRVVQVAcw7rpJbf_I_KLVb-"
      ],
      "env": {
        "DEBUG": "true"
      }
    },
    "Input-pop-up": {
      "command": "/Users/lxj/fastmcp/venv/bin/python3",
      "args": [
        "/Users/lxj/fastmcp/Input-pop-up-window.py"
      ],
      "env": {
        "PYTHONPATH": "/Users/lxj/fastmcp"
      }
    }
  }
}

参考:https://gofastmcp.com/getting-started/installation

相关推荐
共享家952718 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
疯狂的喵18 小时前
C++编译期多态实现
开发语言·c++·算法
2301_7657031418 小时前
C++中的协程编程
开发语言·c++·算法
m0_7487080518 小时前
实时数据压缩库
开发语言·c++·算法
Hgfdsaqwr19 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
lly20240619 小时前
jQuery Mobile 表格
开发语言
一晌小贪欢19 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模19 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
惊讶的猫19 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
Halo_tjn19 小时前
基于封装的专项 知识点
java·前端·python·算法