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

相关推荐
我鑫如一2 分钟前
专业的AI API中转站厂家
人工智能·python
沐知全栈开发3 分钟前
jQuery Mobile 事件详解
开发语言
如竟没有火炬4 分钟前
接雨水22
数据结构·python·算法·leetcode·散列表
消晨消晨5 分钟前
Pytorch初上手——Dataset自定义数据集与Dataloader数据加载器
人工智能·pytorch·python
小白学大数据18 分钟前
均线选股策略研究:基于 Python 数据分析实现
人工智能·python·数据分析
C137的本贾尼24 分钟前
从零认识 Spring AI:Java 开发者的 AI 第一课
python·langchain
源码之家25 分钟前
计算机毕业设计:Pyhon健康数据分析系统 Django框架 数据分析 可视化 身体数据分析 大数据(建议收藏)✅
大数据·python·数据挖掘·数据分析·django·lstm·课程设计
知识分享小能手31 分钟前
R语言入门学习教程,从入门到精通,R语言获取数据 (8)
开发语言·学习·r语言
qq_4523962331 分钟前
第十一篇:《性能压测基础:JMeter线程模型与压测策略设计》
java·开发语言·jmeter
ComputerInBook32 分钟前
C++ 关键字 constexpr 和 consteval 之注意事项
开发语言·c++·constexpr·consteval