找回密码
 立即注册
首页 业界区 业界 如何使用MCP开发一个客户端和服务端

如何使用MCP开发一个客户端和服务端

肿抢 3 天前
 
如何使用MCP开发一个客户端和服务端

一、MCP和API以及Function Call核心概念对比

                  
特性APIFunction CallMCP (Model Context Protocol)
定位通用应用程序接口大模型原生扩展能力标准化模型-服务交互协议
耦合度与具体服务绑定与模型强绑定 (如 GPT-4-turbo)与模型解耦,跨平台通用
交互模式直接请求-响应模型生成结构化调用建议JSON-RPC 2.0 标准化通信
典型场景数据集成、微服务通信简单实时操作 (天气/订单查询)复杂异步任务 & 跨系统整合
              
二、 MCP 协议

1. 什么是MCP协议

模型上下文协议(Model Context Protocol)是一种专为大语言模型设计的标准化协议,它允许LLM以安全、一致的方式与外部系统交互。MCP协议常被描述为"AI的USB-C接口",提供了一种统一的方式连接LLM与它们可以使用的资源。
MCP协议的核心功能包括:

  • 资源(Resources):类似于GET端点,用于将信息加载到LLM的上下文中
  • 工具(Tools):类似于POST端点,用于执行代码或产生副作用
  • 提示(Prompts):可重用的LLM交互模板
  • 上下文(Context):提供额外的交互功能,如日志记录和进度报告
2. 核心价值


  • 标准化:统一 AI 与外部服务的交互格式,解决工具碎片化问题
  • 解耦设计:模型无需硬编码 API 逻辑,通过声明式函数描述调用服务
  • 异步支持:适用于多步骤工作流(如爬取数据→分析→存储)
3. 工作流程

MCP 大概的工作方式: Claude Desktop、Cursor 这些工具,在内部实现了 MCP Client,然后MCP Client 通过标准的 MCP 协议和 MCP Server 进行交互,由各种三方开发者提供的 MCP Server 负责实现各种和三方资源交互的逻辑,比如访问数据库、浏览器、本地文件,最终再通过 标准的 MCP 协议返回给 MCP Client,最终给用户进行展示。
下图是一个通过查询天气来简单展示其对应的工作方式:
  1. <svg aria-roledescription="sequence" role="graphics-document document" viewBox="-50 -10 1204 575"  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="100%" id="mermaid-1749113070264"><g><rect  ry="3" rx="3" name="External_Service" height="65" width="150" stroke="#666" fill="#eaeaea" y="489" x="954" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="521.5" x="1029"><tspan dy="0" x="1029">External_Service</tspan></text></g><g><rect  ry="3" rx="3" name="MCP_Server" height="65" width="150" stroke="#666" fill="#eaeaea" y="489" x="754" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="521.5" x="829"><tspan dy="0" x="829">MCP_Server</tspan></text></g><g><rect  ry="3" rx="3" name="AI_Model" height="65" width="150" stroke="#666" fill="#eaeaea" y="489" x="280" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="521.5" x="355"><tspan dy="0" x="355">AI_Model</tspan></text></g><g><rect  ry="3" rx="3" name="User" height="65" width="150" stroke="#666" fill="#eaeaea" y="489" x="0" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="521.5" x="75"><tspan dy="0" x="75">User</tspan></text></g><g><line name="External_Service" stroke="#999" stroke-width="0.5px"  y2="489" x2="1029" y1="65" x1="1029" id="actor3" ></line><g id="root-3"><rect  ry="3" rx="3" name="External_Service" height="65" width="150" stroke="#666" fill="#eaeaea" y="0" x="954" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="32.5" x="1029"><tspan dy="0" x="1029">External_Service</tspan></text></g></g><g><line name="MCP_Server" stroke="#999" stroke-width="0.5px"  y2="489" x2="829" y1="65" x1="829" id="actor2" ></line><g id="root-2"><rect  ry="3" rx="3" name="MCP_Server" height="65" width="150" stroke="#666" fill="#eaeaea" y="0" x="754" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="32.5" x="829"><tspan dy="0" x="829">MCP_Server</tspan></text></g></g><g><line name="AI_Model" stroke="#999" stroke-width="0.5px"  y2="489" x2="355" y1="65" x1="355" id="actor1" ></line><g id="root-1"><rect  ry="3" rx="3" name="AI_Model" height="65" width="150" stroke="#666" fill="#eaeaea" y="0" x="280" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="32.5" x="355"><tspan dy="0" x="355">AI_Model</tspan></text></g></g><g><line name="User" stroke="#999" stroke-width="0.5px"  y2="489" x2="75" y1="65" x1="75" id="actor0" ></line><g id="root-0"><rect  ry="3" rx="3" name="User" height="65" width="150" stroke="#666" fill="#eaeaea" y="0" x="0" ></rect><text   alignment-baseline="central" dominant-baseline="central" y="32.5" x="75"><tspan dy="0" x="75">User</tspan></text></g></g><g></g><defs><symbol height="24" width="24" id="computer"><path d="M2 2v13h20v-13h-20zm18 11h-16v-9h16v9zm-10.228 6l.466-1h3.524l.467 1h-4.457zm14.228 3h-24l2-6h2.104l-1.33 4h18.45l-1.297-4h2.073l2 6zm-5-10h-14v-7h14v7z" transform="scale(.5)"></path></symbol></defs><defs><symbol clip-rule="evenodd" fill-rule="evenodd" id="database"><path d="M12.258.001l.256.004.255.005.253.008.251.01.249.012.247.015.246.016.242.019.241.02.239.023.236.024.233.027.231.028.229.031.225.032.223.034.22.036.217.038.214.04.211.041.208.043.205.045.201.046.198.048.194.05.191.051.187.053.183.054.18.056.175.057.172.059.168.06.163.061.16.063.155.064.15.066.074.033.073.033.071.034.07.034.069.035.068.035.067.035.066.035.064.036.064.036.062.036.06.036.06.037.058.037.058.037.055.038.055.038.053.038.052.038.051.039.05.039.048.039.047.039.045.04.044.04.043.04.041.04.04.041.039.041.037.041.036.041.034.041.033.042.032.042.03.042.029.042.027.042.026.043.024.043.023.043.021.043.02.043.018.044.017.043.015.044.013.044.012.044.011.045.009.044.007.045.006.045.004.045.002.045.001.045v17l-.001.045-.002.045-.004.045-.006.045-.007.045-.009.044-.011.045-.012.044-.013.044-.015.044-.017.043-.018.044-.02.043-.021.043-.023.043-.024.043-.026.043-.027.042-.029.042-.03.042-.032.042-.033.042-.034.041-.036.041-.037.041-.039.041-.04.041-.041.04-.043.04-.044.04-.045.04-.047.039-.048.039-.05.039-.051.039-.052.038-.053.038-.055.038-.055.038-.058.037-.058.037-.06.037-.06.036-.062.036-.064.036-.064.036-.066.035-.067.035-.068.035-.069.035-.07.034-.071.034-.073.033-.074.033-.15.066-.155.064-.16.063-.163.061-.168.06-.172.059-.175.057-.18.056-.183.054-.187.053-.191.051-.194.05-.198.048-.201.046-.205.045-.208.043-.211.041-.214.04-.217.038-.22.036-.223.034-.225.032-.229.031-.231.028-.233.027-.236.024-.239.023-.241.02-.242.019-.246.016-.247.015-.249.012-.251.01-.253.008-.255.005-.256.004-.258.001-.258-.001-.256-.004-.255-.005-.253-.008-.251-.01-.249-.012-.247-.015-.245-.016-.243-.019-.241-.02-.238-.023-.236-.024-.234-.027-.231-.028-.228-.031-.226-.032-.223-.034-.22-.036-.217-.038-.214-.04-.211-.041-.208-.043-.204-.045-.201-.046-.198-.048-.195-.05-.19-.051-.187-.053-.184-.054-.179-.056-.176-.057-.172-.059-.167-.06-.164-.061-.159-.063-.155-.064-.151-.066-.074-.033-.072-.033-.072-.034-.07-.034-.069-.035-.068-.035-.067-.035-.066-.035-.064-.036-.063-.036-.062-.036-.061-.036-.06-.037-.058-.037-.057-.037-.056-.038-.055-.038-.053-.038-.052-.038-.051-.039-.049-.039-.049-.039-.046-.039-.046-.04-.044-.04-.043-.04-.041-.04-.04-.041-.039-.041-.037-.041-.036-.041-.034-.041-.033-.042-.032-.042-.03-.042-.029-.042-.027-.042-.026-.043-.024-.043-.023-.043-.021-.043-.02-.043-.018-.044-.017-.043-.015-.044-.013-.044-.012-.044-.011-.045-.009-.044-.007-.045-.006-.045-.004-.045-.002-.045-.001-.045v-17l.001-.045.002-.045.004-.045.006-.045.007-.045.009-.044.011-.045.012-.044.013-.044.015-.044.017-.043.018-.044.02-.043.021-.043.023-.043.024-.043.026-.043.027-.042.029-.042.03-.042.032-.042.033-.042.034-.041.036-.041.037-.041.039-.041.04-.041.041-.04.043-.04.044-.04.046-.04.046-.039.049-.039.049-.039.051-.039.052-.038.053-.038.055-.038.056-.038.057-.037.058-.037.06-.037.061-.036.062-.036.063-.036.064-.036.066-.035.067-.035.068-.035.069-.035.07-.034.072-.034.072-.033.074-.033.151-.066.155-.064.159-.063.164-.061.167-.06.172-.059.176-.057.179-.056.184-.054.187-.053.19-.051.195-.05.198-.048.201-.046.204-.045.208-.043.211-.041.214-.04.217-.038.22-.036.223-.034.226-.032.228-.031.231-.028.234-.027.236-.024.238-.023.241-.02.243-.019.245-.016.247-.015.249-.012.251-.01.253-.008.255-.005.256-.004.258-.001.258.001zm-9.258 20.499v.01l.001.021.003.021.004.022.005.021.006.022.007.022.009.023.01.022.011.023.012.023.013.023.015.023.016.024.017.023.018.024.019.024.021.024.022.025.023.024.024.025.052.049.056.05.061.051.066.051.07.051.075.051.079.052.084.052.088.052.092.052.097.052.102.051.105.052.11.052.114.051.119.051.123.051.127.05.131.05.135.05.139.048.144.049.147.047.152.047.155.047.16.045.163.045.167.043.171.043.176.041.178.041.183.039.187.039.19.037.194.035.197.035.202.033.204.031.209.03.212.029.216.027.219.025.222.024.226.021.23.02.233.018.236.016.24.015.243.012.246.01.249.008.253.005.256.004.259.001.26-.001.257-.004.254-.005.25-.008.247-.011.244-.012.241-.014.237-.016.233-.018.231-.021.226-.021.224-.024.22-.026.216-.027.212-.028.21-.031.205-.031.202-.034.198-.034.194-.036.191-.037.187-.039.183-.04.179-.04.175-.042.172-.043.168-.044.163-.045.16-.046.155-.046.152-.047.148-.048.143-.049.139-.049.136-.05.131-.05.126-.05.123-.051.118-.052.114-.051.11-.052.106-.052.101-.052.096-.052.092-.052.088-.053.083-.051.079-.052.074-.052.07-.051.065-.051.06-.051.056-.05.051-.05.023-.024.023-.025.021-.024.02-.024.019-.024.018-.024.017-.024.015-.023.014-.024.013-.023.012-.023.01-.023.01-.022.008-.022.006-.022.006-.022.004-.022.004-.021.001-.021.001-.021v-4.127l-.077.055-.08.053-.083.054-.085.053-.087.052-.09.052-.093.051-.095.05-.097.05-.1.049-.102.049-.105.048-.106.047-.109.047-.111.046-.114.045-.115.045-.118.044-.12.043-.122.042-.124.042-.126.041-.128.04-.13.04-.132.038-.134.038-.135.037-.138.037-.139.035-.142.035-.143.034-.144.033-.147.032-.148.031-.15.03-.151.03-.153.029-.154.027-.156.027-.158.026-.159.025-.161.024-.162.023-.163.022-.165.021-.166.02-.167.019-.169.018-.169.017-.171.016-.173.015-.173.014-.175.013-.175.012-.177.011-.178.01-.179.008-.179.008-.181.006-.182.005-.182.004-.184.003-.184.002h-.37l-.184-.002-.184-.003-.182-.004-.182-.005-.181-.006-.179-.008-.179-.008-.178-.01-.176-.011-.176-.012-.175-.013-.173-.014-.172-.015-.171-.016-.17-.017-.169-.018-.167-.019-.166-.02-.165-.021-.163-.022-.162-.023-.161-.024-.159-.025-.157-.026-.156-.027-.155-.027-.153-.029-.151-.03-.15-.03-.148-.031-.146-.032-.145-.033-.143-.034-.141-.035-.14-.035-.137-.037-.136-.037-.134-.038-.132-.038-.13-.04-.128-.04-.126-.041-.124-.042-.122-.042-.12-.044-.117-.043-.116-.045-.113-.045-.112-.046-.109-.047-.106-.047-.105-.048-.102-.049-.1-.049-.097-.05-.095-.05-.093-.052-.09-.051-.087-.052-.085-.053-.083-.054-.08-.054-.077-.054v4.127zm0-5.654v.011l.001.021.003.021.004.021.005.022.006.022.007.022.009.022.01.022.011.023.012.023.013.023.015.024.016.023.017.024.018.024.019.024.021.024.022.024.023.025.024.024.052.05.056.05.061.05.066.051.07.051.075.052.079.051.084.052.088.052.092.052.097.052.102.052.105.052.11.051.114.051.119.052.123.05.127.051.131.05.135.049.139.049.144.048.147.048.152.047.155.046.16.045.163.045.167.044.171.042.176.042.178.04.183.04.187.038.19.037.194.036.197.034.202.033.204.032.209.03.212.028.216.027.219.025.222.024.226.022.23.02.233.018.236.016.24.014.243.012.246.01.249.008.253.006.256.003.259.001.26-.001.257-.003.254-.006.25-.008.247-.01.244-.012.241-.015.237-.016.233-.018.231-.02.226-.022.224-.024.22-.025.216-.027.212-.029.21-.03.205-.032.202-.033.198-.035.194-.036.191-.037.187-.039.183-.039.179-.041.175-.042.172-.043.168-.044.163-.045.16-.045.155-.047.152-.047.148-.048.143-.048.139-.05.136-.049.131-.05.126-.051.123-.051.118-.051.114-.052.11-.052.106-.052.101-.052.096-.052.092-.052.088-.052.083-.052.079-.052.074-.051.07-.052.065-.051.06-.05.056-.051.051-.049.023-.025.023-.024.021-.025.02-.024.019-.024.018-.024.017-.024.015-.023.014-.023.013-.024.012-.022.01-.023.01-.023.008-.022.006-.022.006-.022.004-.021.004-.022.001-.021.001-.021v-4.139l-.077.054-.08.054-.083.054-.085.052-.087.053-.09.051-.093.051-.095.051-.097.05-.1.049-.102.049-.105.048-.106.047-.109.047-.111.046-.114.045-.115.044-.118.044-.12.044-.122.042-.124.042-.126.041-.128.04-.13.039-.132.039-.134.038-.135.037-.138.036-.139.036-.142.035-.143.033-.144.033-.147.033-.148.031-.15.03-.151.03-.153.028-.154.028-.156.027-.158.026-.159.025-.161.024-.162.023-.163.022-.165.021-.166.02-.167.019-.169.018-.169.017-.171.016-.173.015-.173.014-.175.013-.175.012-.177.011-.178.009-.179.009-.179.007-.181.007-.182.005-.182.004-.184.003-.184.002h-.37l-.184-.002-.184-.003-.182-.004-.182-.005-.181-.007-.179-.007-.179-.009-.178-.009-.176-.011-.176-.012-.175-.013-.173-.014-.172-.015-.171-.016-.17-.017-.169-.018-.167-.019-.166-.02-.165-.021-.163-.022-.162-.023-.161-.024-.159-.025-.157-.026-.156-.027-.155-.028-.153-.028-.151-.03-.15-.03-.148-.031-.146-.033-.145-.033-.143-.033-.141-.035-.14-.036-.137-.036-.136-.037-.134-.038-.132-.039-.13-.039-.128-.04-.126-.041-.124-.042-.122-.043-.12-.043-.117-.044-.116-.044-.113-.046-.112-.046-.109-.046-.106-.047-.105-.048-.102-.049-.1-.049-.097-.05-.095-.051-.093-.051-.09-.051-.087-.053-.085-.052-.083-.054-.08-.054-.077-.054v4.139zm0-5.666v.011l.001.02.003.022.004.021.005.022.006.021.007.022.009.023.01.022.011.023.012.023.013.023.015.023.016.024.017.024.018.023.019.024.021.025.022.024.023.024.024.025.052.05.056.05.061.05.066.051.07.051.075.052.079.051.084.052.088.052.092.052.097.052.102.052.105.051.11.052.114.051.119.051.123.051.127.05.131.05.135.05.139.049.144.048.147.048.152.047.155.046.16.045.163.045.167.043.171.043.176.042.178.04.183.04.187.038.19.037.194.036.197.034.202.033.204.032.209.03.212.028.216.027.219.025.222.024.226.021.23.02.233.018.236.017.24.014.243.012.246.01.249.008.253.006.256.003.259.001.26-.001.257-.003.254-.006.25-.008.247-.01.244-.013.241-.014.237-.016.233-.018.231-.02.226-.022.224-.024.22-.025.216-.027.212-.029.21-.03.205-.032.202-.033.198-.035.194-.036.191-.037.187-.039.183-.039.179-.041.175-.042.172-.043.168-.044.163-.045.16-.045.155-.047.152-.047.148-.048.143-.049.139-.049.136-.049.131-.051.126-.05.123-.051.118-.052.114-.051.11-.052.106-.052.101-.052.096-.052.092-.052.088-.052.083-.052.079-.052.074-.052.07-.051.065-.051.06-.051.056-.05.051-.049.023-.025.023-.025.021-.024.02-.024.019-.024.018-.024.017-.024.015-.023.014-.024.013-.023.012-.023.01-.022.01-.023.008-.022.006-.022.006-.022.004-.022.004-.021.001-.021.001-.021v-4.153l-.077.054-.08.054-.083.053-.085.053-.087.053-.09.051-.093.051-.095.051-.097.05-.1.049-.102.048-.105.048-.106.048-.109.046-.111.046-.114.046-.115.044-.118.044-.12.043-.122.043-.124.042-.126.041-.128.04-.13.039-.132.039-.134.038-.135.037-.138.036-.139.036-.142.034-.143.034-.144.033-.147.032-.148.032-.15.03-.151.03-.153.028-.154.028-.156.027-.158.026-.159.024-.161.024-.162.023-.163.023-.165.021-.166.02-.167.019-.169.018-.169.017-.171.016-.173.015-.173.014-.175.013-.175.012-.177.01-.178.01-.179.009-.179.007-.181.006-.182.006-.182.004-.184.003-.184.001-.185.001-.185-.001-.184-.001-.184-.003-.182-.004-.182-.006-.181-.006-.179-.007-.179-.009-.178-.01-.176-.01-.176-.012-.175-.013-.173-.014-.172-.015-.171-.016-.17-.017-.169-.018-.167-.019-.166-.02-.165-.021-.163-.023-.162-.023-.161-.024-.159-.024-.157-.026-.156-.027-.155-.028-.153-.028-.151-.03-.15-.03-.148-.032-.146-.032-.145-.033-.143-.034-.141-.034-.14-.036-.137-.036-.136-.037-.134-.038-.132-.039-.13-.039-.128-.041-.126-.041-.124-.041-.122-.043-.12-.043-.117-.044-.116-.044-.113-.046-.112-.046-.109-.046-.106-.048-.105-.048-.102-.048-.1-.05-.097-.049-.095-.051-.093-.051-.09-.052-.087-.052-.085-.053-.083-.053-.08-.054-.077-.054v4.153zm8.74-8.179l-.257.004-.254.005-.25.008-.247.011-.244.012-.241.014-.237.016-.233.018-.231.021-.226.022-.224.023-.22.026-.216.027-.212.028-.21.031-.205.032-.202.033-.198.034-.194.036-.191.038-.187.038-.183.04-.179.041-.175.042-.172.043-.168.043-.163.045-.16.046-.155.046-.152.048-.148.048-.143.048-.139.049-.136.05-.131.05-.126.051-.123.051-.118.051-.114.052-.11.052-.106.052-.101.052-.096.052-.092.052-.088.052-.083.052-.079.052-.074.051-.07.052-.065.051-.06.05-.056.05-.051.05-.023.025-.023.024-.021.024-.02.025-.019.024-.018.024-.017.023-.015.024-.014.023-.013.023-.012.023-.01.023-.01.022-.008.022-.006.023-.006.021-.004.022-.004.021-.001.021-.001.021.001.021.001.021.004.021.004.022.006.021.006.023.008.022.01.022.01.023.012.023.013.023.014.023.015.024.017.023.018.024.019.024.02.025.021.024.023.024.023.025.051.05.056.05.06.05.065.051.07.052.074.051.079.052.083.052.088.052.092.052.096.052.101.052.106.052.11.052.114.052.118.051.123.051.126.051.131.05.136.05.139.049.143.048.148.048.152.048.155.046.16.046.163.045.168.043.172.043.175.042.179.041.183.04.187.038.191.038.194.036.198.034.202.033.205.032.21.031.212.028.216.027.22.026.224.023.226.022.231.021.233.018.237.016.241.014.244.012.247.011.25.008.254.005.257.004.26.001.26-.001.257-.004.254-.005.25-.008.247-.011.244-.012.241-.014.237-.016.233-.018.231-.021.226-.022.224-.023.22-.026.216-.027.212-.028.21-.031.205-.032.202-.033.198-.034.194-.036.191-.038.187-.038.183-.04.179-.041.175-.042.172-.043.168-.043.163-.045.16-.046.155-.046.152-.048.148-.048.143-.048.139-.049.136-.05.131-.05.126-.051.123-.051.118-.051.114-.052.11-.052.106-.052.101-.052.096-.052.092-.052.088-.052.083-.052.079-.052.074-.051.07-.052.065-.051.06-.05.056-.05.051-.05.023-.025.023-.024.021-.024.02-.025.019-.024.018-.024.017-.023.015-.024.014-.023.013-.023.012-.023.01-.023.01-.022.008-.022.006-.023.006-.021.004-.022.004-.021.001-.021.001-.021-.001-.021-.001-.021-.004-.021-.004-.022-.006-.021-.006-.023-.008-.022-.01-.022-.01-.023-.012-.023-.013-.023-.014-.023-.015-.024-.017-.023-.018-.024-.019-.024-.02-.025-.021-.024-.023-.024-.023-.025-.051-.05-.056-.05-.06-.05-.065-.051-.07-.052-.074-.051-.079-.052-.083-.052-.088-.052-.092-.052-.096-.052-.101-.052-.106-.052-.11-.052-.114-.052-.118-.051-.123-.051-.126-.051-.131-.05-.136-.05-.139-.049-.143-.048-.148-.048-.152-.048-.155-.046-.16-.046-.163-.045-.168-.043-.172-.043-.175-.042-.179-.041-.183-.04-.187-.038-.191-.038-.194-.036-.198-.034-.202-.033-.205-.032-.21-.031-.212-.028-.216-.027-.22-.026-.224-.023-.226-.022-.231-.021-.233-.018-.237-.016-.241-.014-.244-.012-.247-.011-.25-.008-.254-.005-.257-.004-.26-.001-.26.001z" transform="scale(.5)"></path></symbol></defs><defs><symbol height="24" width="24" id="clock"><path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm5.848 12.459c.202.038.202.333.001.372-1.907.361-6.045 1.111-6.547 1.111-.719 0-1.301-.582-1.301-1.301 0-.512.77-5.447 1.125-7.445.034-.192.312-.181.343.014l.985 6.238 5.394 1.011z" transform="scale(.5)"></path></symbol></defs><defs><marker orient="auto-start-reverse" markerHeight="12" markerWidth="12" markerUnits="userSpaceOnUse" refY="5" refX="7.9" id="arrowhead"><path d="M -1 0 L 10 5 L 0 10 z" ></path></marker></defs><defs><marker refY="4.5" refX="4" orient="auto" markerHeight="8" markerWidth="15" id="crosshead"><path  d="M 1,2 L 6,7 M 6,2 L 1,7" stroke-width="1pt" stroke="#000000" fill="none"></path></marker></defs><defs><marker orient="auto" markerHeight="28" markerWidth="20" refY="7" refX="15.5" id="filled-head"><path d="M 18,7 L9,13 L14,7 L9,1 Z"></path></marker></defs><defs><marker orient="auto" markerHeight="40" markerWidth="60" refY="15" refX="15" id="sequencenumber" ><circle r="6" cy="15" cx="15"></circle></marker></defs><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="80" x="214">“查询北京天气”</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="123" x2="351" y1="123" x1="76"></line><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="138" x="356">解析意图,生成MCP调用</text><path  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  d="M 356,173 C 416,163 416,203 356,193"></path><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="218" x="591">{ "function": "get_weather", "params": {"city":"北京"} }</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="261" x2="825" y1="261" x1="356"></line><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="276" x="928">调用天气API</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="311" x2="1025" y1="311" x1="830"></line><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="326" x="931">返回原始数据</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="361" x2="833" y1="361" x1="1028"></line><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="376" x="594">{ "temperature": "22°C", "condition": "晴" }</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="411" x2="359" y1="411" x1="828"></line><text  dy="1em"  alignment-baseline="middle" dominant-baseline="middle" text-anchor="middle" y="426" x="217">“北京今天晴天,气温22°C”</text><line  marker-end="url(#arrowhead)" stroke="none" stroke-width="2"  y2="469" x2="79" y1="469" x1="354"></line></svg>
复制代码
3. 代码实现mcp客户端和服务端

现在python编写mcp server和mcp client的有两个分别是FastMCP 和MCP,其中MCP是官方的pythonsdk,这两个之间的关系是官方收编了FastMCP的第一个版本的包,但官方集成的是 fastmcp 的 v1.0 版本。然而,jlowin 继续开发 fastmcp,还发布了 v2.0 版本,其中包含代理和客户端采样等新功能。以下的演示以官方版本MCP为例,
安装:uv add "mcp[cli]” 或者pip install "mcp[cli]”
(1) MCP 服务端
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg>from mcp.server.fastmcp import FastMCP
  6. from mcp.server.fastmcp.prompts import base

  7. # mcp = FastMCP(name="demo",host="127.0.0.1",port=8256,sse_path="/sse")   ### 启动方式为sse时使用
  8. mcp = FastMCP()
  9. @mcp.tool()
  10. def add_2_numbers(a: int, b: int) -> int:
  11.  """两个数字相加"""
  12.  return a + b

  13. @mcp.resource("config://app")
  14. def get_config() -> str:
  15.     """Static configuration data"""
  16.     return "App configuration here"

  17. @mcp.prompt()
  18. def debug_error(error: str) -> list[base.Message]:
  19.     return [
  20.         base.UserMessage("I'm seeing this error:"),
  21.         base.UserMessage(error),
  22.         base.AssistantMessage("I'll help debug that. What have you tried so far?"),

  23. @mcp.tool()
  24. def multiply_2_numbers(a: int, b: int):
  25.  """两个数字相乘"""
  26.  return a * b

  27. if __name__ == "__main__":
  28.  # mcp.run(transport='sse')   ## 启动方式为sse
  29.  mcp.run(transport='stdio')   ## 启动方式为stdio
复制代码
解释:

  • • Tools(工具)是MCP中最常用的功能之一,它允许LLM执行特定的操作或函数。使用@mcp.tool()装饰器可以轻松将Python函数转换为LLM可调用的工具:
  • • Resources(资源)用于向LLM提供数据和上下文信息。与工具不同,资源主要用于读取数据而非执行操作
  • • Prompts(提示)允许您创建可重用的提示模板,这些模板可以被参数化并用于标准化LLM交互
简单验证服务端功能可以通过mcp dev server.py进入界面检测
(2) MCP 客户端

MCP客户端一般分别按照服务端的stdio和sse分别写了两个,具体融合的最后修改一下即可。

  • 1. STDIO客户端
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg>import asyncio
  6. import json
  7. import re
  8. from contextlib import AsyncExitStack
  9. from typing import Optional

  10. from lxml import etree
  11. from mcp import ClientSession, StdioServerParameters, stdio_client
  12. from mcp.client.sse import sse_client
  13. from openai import AsyncOpenAI

  14. class Stdio_MCPClient():

  15.     def __init__(self,api_key, base_url, model):
  16.         self.session: Optional[ClientSession] = None
  17.         self.exit_stack = AsyncExitStack()
  18.         self.client = AsyncOpenAI(api_key=api_key, base_url=base_url)
  19.         self.model = model
  20.         self.message = []
  21.         with open("MCP_Prompt.txt", "r", encoding="utf-8") as f:
  22.             self.system_prompt = f.read()

  23.     async def connect_to_stdio_server(self, mcp_name, command,args,env={}):
  24.         server_params = StdioServerParameters(
  25.             command=command,
  26.             args=args,
  27.             env=env
  28.         )
  29.         stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
  30.         self.stdio,self.write = stdio_transport
  31.         self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
  32.         await self.session.initialize()
  33.         response = await self.session.list_tools()
  34.         tools = response.tools
  35.         print(f"成功链接到{mcp_name}服务,对应的tools:",[tool.name for tool in tools])
  36.         self.available_tools = [{
  37.             "type": "function",
  38.             "function": {
  39.                 "name": tool.name,
  40.                 "description": tool.description,
  41.                 "parameters": tool.inputSchema
  42.             }
  43.         } for tool in response.tools]

  44.     async def process_query(self, query: str, stream: bool = False):
  45.         # self.message.append({"role": "system", "content": self.system_prompt})
  46.         self.message.append({"role": "user", "content": query})
  47.         response = await self.client.chat.completions.create(
  48.             model=self.model,
  49.             messages=self.message,
  50.             tools=self.available_tools
  51.         )
  52.         final_text = []
  53.         assistant_message = response.choices[0].message
  54.         while assistant_message.tool_calls:
  55.             for tool_call in assistant_message.tool_calls:
  56.                 tool_name = tool_call.function.name
  57.                 tool_args = json.loads(tool_call.function.arguments)

  58.                 result = await self.session.call_tool(tool_name, tool_args)
  59.                 print(f"calling tools {tool_name},wirh args {tool_args}")
  60.                 print("Result:", result.content[0].text)
  61.                 self.message.extend([
  62.                     {
  63.                         "role": "assistant",
  64.                         "content": None,
  65.                         "tool_calls": [tool_call]
  66.                     },
  67.                     {
  68.                         "role": "tool",
  69.                         "content": result.content[0].text,
  70.                         "tool_call_id": tool_call.id
  71.                     }
  72.                 ])
  73.             response = await self.client.chat.completions.create(
  74.                 model=self.model,
  75.                 messages=self.message,
  76.                 tools=self.available_tools,
  77.                 max_tokens=8048

  78.             )
  79.             assistant_message = response.choices[0].message
  80.         content = assistant_message.content
  81.         final_text.append(content)

  82.         return "\n".join(final_text)

  83.     async def chat_loop(self,stream_mode=True):
  84.         self.message = []
  85.         while True:
  86.             try:
  87.                 query = input("\nQuery: ").strip()
  88.                 if query.lower() == 'quit':
  89.                     break
  90.                 if query.strip() == '':
  91.                     continue
  92.                 response = await self.process_query(query, stream=stream_mode)
  93.                 print("\nAI:", response)
  94.             except Exception as e:
  95.                 print(f"\nError: {str(e)}")

  96.     async def cleanup(self):
  97.         await self.exit_stack.aclose()

  98. async def main():
  99.     with open("config.json", "r") as f:
  100.         config = json.load(f)
  101.     client = Stdio_MCPClient(config["llm"]["api_key"], config["llm"]["base_url"], config["llm"]["model"])
  102.     try:
  103.         env = {}
  104.         await client.connect_to_stdio_server("testserver","python",["server.py",],{})
  105.         await client.chat_loop()
  106.     except  Exception as e:
  107.         print(e)
  108.     finally:
  109.         await client.cleanup()

  110. if __name__ == '__main__':
  111.     asyncio.run(main())
复制代码

  • 1. SSE客户端
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg>import asyncio
  6. import json
  7. import re
  8. from contextlib import AsyncExitStack
  9. from typing import Optional

  10. from lxml import etree
  11. from mcp import ClientSession, StdioServerParameters, stdio_client
  12. from mcp.client.sse import sse_client
  13. from openai import AsyncOpenAI

  14. class SSE_MCPClient():

  15.     def __init__(self,api_key, base_url, model):
  16.         self.session: Optional[ClientSession] = None
  17.         self.exit_stack = AsyncExitStack()
  18.         self.client = AsyncOpenAI(api_key=api_key, base_url=base_url)
  19.         self.model = model
  20.         self.message = []
  21.         with open("MCP_Prompt.txt", "r", encoding="utf-8") as f:
  22.             self.system_prompt = f.read()

  23.        async def connect_to_sse_server(self, mcp_name, server_url,headers=None):
  24.         self.service = [{
  25.             "name": mcp_name,
  26.             "url": server_url,
  27.             "headers": headers
  28.         }]
  29.         sse_transport = await self.exit_stack.enter_async_context(sse_client(server_url, headers,timeout=30,sse_read_timeout=30))
  30.         self.sse,self.write = sse_transport
  31.         self.session = await self.exit_stack.enter_async_context(ClientSession(self.sse, self.write))
  32.         await self.session.initialize()
  33.         response = await self.session.list_tools()
  34.         tools = response.tools
  35.         print(f"成功链接到{mcp_name}服务,对应的tools:",[tool.name for tool in tools])
  36.         self.available_tools = [{
  37.             "type": "function",
  38.             "function": {
  39.                 "name": tool.name,
  40.                 "description": tool.description,
  41.                 "parameters": tool.inputSchema
  42.             }
  43.         } for tool in response.tools]

  44.     async def reconnect_sse_server(self):
  45.         for service in self.service:
  46.             mcp_name = service["name"]
  47.             server_url = service["url"]
  48.             headers = service.get("headers", None)
  49.             sse_transport = await self.exit_stack.enter_async_context(sse_client(server_url, headers))
  50.             self.sse, self.write = sse_transport
  51.             self.session = await self.exit_stack.enter_async_context(ClientSession(self.sse, self.write))
  52.             await self.session.initialize()
  53.             print(f"重新成功链接到 {mcp_name} 服务")
  54.             
  55.     async def process_query(self, query: str, stream: bool = False):
  56.         # self.message.append({"role": "system", "content": self.system_prompt})
  57.         self.message.append({"role": "user", "content": query})
  58.         response = await self.client.chat.completions.create(
  59.             model=self.model,
  60.             messages=self.message,
  61.             tools=self.available_tools
  62.         )
  63.         final_text = []
  64.         assistant_message = response.choices[0].message
  65.         while assistant_message.tool_calls:
  66.             for tool_call in assistant_message.tool_calls:
  67.                 tool_name = tool_call.function.name
  68.                 tool_args = json.loads(tool_call.function.arguments)
  69.                                 await self.reconnect_sse_server()
  70.                 result = await self.session.call_tool(tool_name, tool_args)
  71.                 print(f"calling tools {tool_name},wirh args {tool_args}")
  72.                 print("Result:", result.content[0].text)
  73.                 self.message.extend([
  74.                     {
  75.                         "role": "assistant",
  76.                         "content": None,
  77.                         "tool_calls": [tool_call]
  78.                     },
  79.                     {
  80.                         "role": "tool",
  81.                         "content": result.content[0].text,
  82.                         "tool_call_id": tool_call.id
  83.                     }
  84.                 ])
  85.             response = await self.client.chat.completions.create(
  86.                 model=self.model,
  87.                 messages=self.message,
  88.                 tools=self.available_tools,
  89.                 max_tokens=8048

  90.             )
  91.             assistant_message = response.choices[0].message
  92.         content = assistant_message.content
  93.         final_text.append(content)

  94.         return "\n".join(final_text)

  95.     async def chat_loop(self,stream_mode=True):
  96.         self.message = []
  97.         while True:
  98.             try:
  99.                 query = input("\nQuery: ").strip()
  100.                 if query.lower() == 'quit':
  101.                     break
  102.                 if query.strip() == '':
  103.                     continue
  104.                 response = await self.process_query(query, stream=stream_mode)
  105.                 print("\nAI:", response)
  106.             except Exception as e:
  107.                 print(f"\nError: {str(e)}")

  108.     async def cleanup(self):
  109.         await self.exit_stack.aclose()

  110. async def main():
  111.     with open("config.json", "r") as f:
  112.         config = json.load(f)
  113.     client = Stdio_MCPClient(config["llm"]["api_key"], config["llm"]["base_url"], config["llm"]["model"])
  114.     try:
  115.        await client.connect_to_sse_server(mcp_name="test", server_url="https://127.0.0.1:7860/sse")
  116.         await client.chat_loop()
  117.     except  Exception as e:
  118.         print(e)
  119.     finally:
  120.         await client.cleanup()

  121. if __name__ == '__main__':
  122.     asyncio.run(main())
复制代码
注意: sse链接,我增加了一个reconnect_sse_server 函数,主要原因是sse链接过程中过2分钟会自然断开,不论什么办法都无法处理,因此增加这样一个操作。
(3)版本的自然更新

有了上面两种客户端的连接方法,自然而然结合两个就可以做到同时结合sse和stdio的方法只需要增加一个分别调用的方法即可,后续代码微微改动便可使用。
当然官方的MCP也是在不段更新的,看了官方有发布Streamable HTTP Transport ,这种方式在取代sse,以及通过with来启动执行服务的更新等等,一些简单的更新参考下面,更多更新可以前往github上看
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg>from mcp.server.fastmcp import FastMCP

  6. # Stateful server (maintains session state)
  7. mcp = FastMCP("StatefulServer")

  8. # Stateless server (no session persistence)
  9. mcp = FastMCP("StatelessServer", stateless_http=True)

  10. # Stateless server (no session persistence, no sse stream with supported client)
  11. mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)

  12. # Run server with streamable_http transport
  13. mcp.run(transport="streamable-http")
复制代码
其余高级用法可参考页面:https://github.com/modelcontextprotocol/python-sdk#advanced-usage
三、典型应用场景

1. MCP 适用场景


  • 企业系统整合  将 CRM/ERP 封装为 MCP 服务,供多个 Agent 安全调用
    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
    2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
    3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
    4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
    5.   </svg># MCP 连接数据库示例
    6. @app.post("/mcp")
    7. def query_database(request: dict):
    8.     if request["function"] == "get_user_orders":
    9.         user_id = request["parameters"]["user_id"]
    10.         # 执行SQL查询 (伪代码)
    11.         return {"orders": db.query(f"SELECT * FROM orders WHERE user_id={user_id}")}
    复制代码
  • 跨平台自动化  组合 GitHub + Slack 的 MCP 服务实现 CI/CD 流程:
    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
    2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
    3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
    4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
    5.   </svg># 自动化工作流:提交代码→构建→通知
    6. def ci_cd_pipeline():
    7.     call_mcp("github", {"action": "pull_code", "repo": "my-app"})
    8.     build_result = call_mcp("jenkins", {"job": "build"})
    9.     call_mcp("slack", {"channel": "dev-team", "message": f"构建结果:{build_result}"})
    复制代码
2. Function Call 适用场景
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg># 简单实时查询(无需MCP)
  6. def get_stock_price(symbol: str):
  7.     return yahoo_finance_api(symbol)

  8. # 注册函数到模型
  9. functions = [{
  10.     "name": "get_stock_price",
  11.     "parameters": {"symbol": {"type": "string"}}
  12. }]

  13. # 模型直接调用
  14. response = model.generate("AAPL当前股价?", functions=functions)
  15. if response.function_call:
  16.     print(get_stock_price(response.function_call.arguments["symbol"]))
复制代码
3. 传统 API 调用
  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
  2.     <ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
  3.     <ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
  4.     <ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
  5.   </svg># 直接调用 REST API(无AI参与)
  6. import requests
  7. def fetch_weather(city: str):
  8.     response = requests.get(f"https://api.weather.com/v1/{city}")
  9.     return response.json()["temperature"]
复制代码

四、技术选型建议

                  
场景推荐方案原因
简单同步任务(天气/股价查询)Function Call低延迟,与模型紧密集成
跨系统异步任务(数据分析流水线)MCP标准化协议支持复杂工作流
企业内部系统暴露服务MCP统一认证 + 访问控制
第三方公共服务调用API + Function Call无需额外协议层
              
关键结论:MCP 的核心价值在于建立企业级 AI 基础设施。当系统需要连接多个异构数据源、要求严格的协议标准化或涉及长周期任务时,MCP 是优于 Function Call 的选择。


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册