my_mcp_server.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """
  2. 自定义MCP服务器示例
  3. 这是一个简单的MCP服务器,提供基础的数学计算和文本处理工具。
  4. 用于演示如何创建自己的MCP服务器。
  5. 运行方式:
  6. python my_mcp_server.py
  7. 或者作为MCP服务器被客户端调用:
  8. MCPClient(["python", "my_mcp_server.py"])
  9. """
  10. from fastmcp import FastMCP
  11. import sys
  12. import os
  13. # 创建MCP服务器实例
  14. mcp = FastMCP("MyCustomServer")
  15. # ==================== 数学工具 ====================
  16. @mcp.tool()
  17. def add(a: float, b: float) -> float:
  18. """
  19. 加法计算器
  20. Args:
  21. a: 第一个数字
  22. b: 第二个数字
  23. Returns:
  24. 两数之和
  25. """
  26. return a + b
  27. @mcp.tool()
  28. def subtract(a: float, b: float) -> float:
  29. """
  30. 减法计算器
  31. Args:
  32. a: 被减数
  33. b: 减数
  34. Returns:
  35. 两数之差
  36. """
  37. return a - b
  38. @mcp.tool()
  39. def multiply(a: float, b: float) -> float:
  40. """
  41. 乘法计算器
  42. Args:
  43. a: 第一个数字
  44. b: 第二个数字
  45. Returns:
  46. 两数之积
  47. """
  48. return a * b
  49. @mcp.tool()
  50. def divide(a: float, b: float) -> float:
  51. """
  52. 除法计算器
  53. Args:
  54. a: 被除数
  55. b: 除数
  56. Returns:
  57. 两数之商
  58. Raises:
  59. ValueError: 当除数为0时
  60. """
  61. if b == 0:
  62. raise ValueError("除数不能为零")
  63. return a / b
  64. # ==================== 文本处理工具 ====================
  65. @mcp.tool()
  66. def reverse_text(text: str) -> str:
  67. """
  68. 反转文本
  69. Args:
  70. text: 要反转的文本
  71. Returns:
  72. 反转后的文本
  73. """
  74. return text[::-1]
  75. @mcp.tool()
  76. def count_words(text: str) -> int:
  77. """
  78. 统计文本中的单词数量
  79. Args:
  80. text: 要统计的文本
  81. Returns:
  82. 单词数量
  83. """
  84. return len(text.split())
  85. @mcp.tool()
  86. def to_uppercase(text: str) -> str:
  87. """
  88. 将文本转换为大写
  89. Args:
  90. text: 要转换的文本
  91. Returns:
  92. 大写文本
  93. """
  94. return text.upper()
  95. @mcp.tool()
  96. def to_lowercase(text: str) -> str:
  97. """
  98. 将文本转换为小写
  99. Args:
  100. text: 要转换的文本
  101. Returns:
  102. 小写文本
  103. """
  104. return text.lower()
  105. # ==================== 资源定义 ====================
  106. @mcp.resource("config://server")
  107. def get_server_config() -> str:
  108. """
  109. 获取服务器配置信息
  110. Returns:
  111. 服务器配置的JSON字符串
  112. """
  113. import json
  114. config = {
  115. "name": "MyCustomServer",
  116. "version": "1.0.0",
  117. "tools_count": 8,
  118. "description": "自定义MCP服务器示例"
  119. }
  120. return json.dumps(config, ensure_ascii=False, indent=2)
  121. @mcp.resource("info://capabilities")
  122. def get_capabilities() -> str:
  123. """
  124. 获取服务器能力列表
  125. Returns:
  126. 能力列表的文本描述
  127. """
  128. capabilities = """
  129. 服务器能力列表:
  130. 数学计算:
  131. - add: 加法计算
  132. - subtract: 减法计算
  133. - multiply: 乘法计算
  134. - divide: 除法计算
  135. 文本处理:
  136. - reverse_text: 反转文本
  137. - count_words: 统计单词数
  138. - to_uppercase: 转换为大写
  139. - to_lowercase: 转换为小写
  140. 资源:
  141. - config://server: 服务器配置
  142. - info://capabilities: 能力列表(本资源)
  143. """
  144. return capabilities.strip()
  145. # ==================== 提示词模板 ====================
  146. @mcp.prompt()
  147. def math_helper() -> str:
  148. """
  149. 数学计算助手提示词
  150. Returns:
  151. 提示词模板
  152. """
  153. return """你是一个数学计算助手。你可以使用以下工具:
  154. - add(a, b): 计算两数之和
  155. - subtract(a, b): 计算两数之差
  156. - multiply(a, b): 计算两数之积
  157. - divide(a, b): 计算两数之商
  158. 请根据用户的问题选择合适的工具进行计算。"""
  159. @mcp.prompt()
  160. def text_processor() -> str:
  161. """
  162. 文本处理助手提示词
  163. Returns:
  164. 提示词模板
  165. """
  166. return """你是一个文本处理助手。你可以使用以下工具:
  167. - reverse_text(text): 反转文本
  168. - count_words(text): 统计单词数
  169. - to_uppercase(text): 转换为大写
  170. - to_lowercase(text): 转换为小写
  171. 请根据用户的需求选择合适的工具处理文本。"""
  172. # ==================== 主程序 ====================
  173. if __name__ == "__main__":
  174. # 运行MCP服务器
  175. # FastMCP会自动处理stdio传输
  176. mcp.run()