当前位置: 首页> 房产> 政策 > 青岛销售系统app开发_宁波网络推广方法_免费网站seo诊断_手机百度官网首页

青岛销售系统app开发_宁波网络推广方法_免费网站seo诊断_手机百度官网首页

时间:2025/7/16 6:45:08来源:https://blog.csdn.net/qq_35204012/article/details/147255066 浏览次数:0次
青岛销售系统app开发_宁波网络推广方法_免费网站seo诊断_手机百度官网首页

 代码先行

import asyncio
import json
import osfrom mcp.server import Server
from mcp.types import Resource, Tool, TextContent
from typing import Listapp = Server("file_list")@app.list_tools()
async def list_tools() -> List[Tool]:"""List available file tools."""return [Tool(name="list_files",description="List files in the given directory.",inputSchema={"type": "object","properties": {"text": {"type": "string","description": "The directory path to list files from."}},"required": ["text"]}),Tool(name="rename_file",description="rename the file name to a new name.",inputSchema={"type": "object","properties": {"old_name": {"type": "string","description": "the file's old name."},"new_name": {"type": "string","description": "the file's new name."}},"required": ["old_name", "new_name"]})]@app.call_tool()
async def list_files(name: str, path: dict) -> List[TextContent]:"""List files in the given directory."""try:directory = path['text']files = os.listdir(directory)return [{"type": "text","text": f"文件夹内包含的文件和文件夹有:{','.join(files)}"}]except Exception as e:raise RuntimeError(f"Error {name} in {path}: {str(e)}")@app.call_tool()
async def rename_file(name: str, path: dict) -> List[TextContent]:"""Rename a file."""old_name = path['old_name']new_name = path['new_name']os.rename(old_name, new_name)return [{"type": "text","text": f"文件 {old_name} 重命名为 {new_name} 成功。"}]async def main():"""Main entry point to run the MCP server."""from mcp.server.stdio import stdio_serverasync with stdio_server() as (read_stream, write_stream):try:await app.run(read_stream,write_stream,app.create_initialization_options())except Exception as e:raiseif __name__ == "__main__":asyncio.run(main())

核心点有三个

1.list_tools 这个是给AI展示有哪些工具可以调用的,具体工具的功能是什么,可以在这里面配置具体工具call_tools的入参数据类型,确保执行的健壮性

2.call_tool 这个是具体的实现方法,定义的名称与list_tools要一致,里面是一些具体的实现方法

3.main 这个是用来启动整个服务器的,主要是对标准数据输入输出流进行适配

使用效果

关键字:青岛销售系统app开发_宁波网络推广方法_免费网站seo诊断_手机百度官网首页

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: