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

相关推荐
代码山河5 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
在世修行5 小时前
智学文枢:题目流水线
python·多线程·状态机·pyside
C^h5 小时前
c++ string
开发语言·c++
raindayinrain5 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
梯度下降者5 小时前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
学逆向的5 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao5 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
学逆向的6 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的7 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc