新闻详情

新闻详情

首页 / 资讯中心 / 详情

自定义一个简单的MCP Server:使用SpringAI + Cline + DeepSeek实现AI创建文件并写入内容

发布时间:2026/9/27 7:38:41来源:尧图网络
自定义一个简单的MCP Server:使用SpringAI + Cline + DeepSeek实现AI创建文件并写入内容
实现功能通过与AI的对话让AI自动完成将指定内容写入指定目标文件的功能效果如下环境windows 11、jdk 17、SpringBoot 3.4.4步骤1、安装vscode并安装Cline插件2、在Cline插件中配置大模型Cline可以配置多种大模型比如本地的Ollama、阿里的千问、deepseek等等这里以配置deepseek为例先去deepseek开放平台注册账号然后随便充值十来块钱购买使用量然后创建api-key接着就可以在Cline中配置deepseek了配置好后可以返回Cline的对话界面问一个简单的问题试一下大模型是否生效注意对话界面的右下角有Plan、Act两个选项Plan可以理解为一般的对话聊天Act就是需要大模型配合MCP服务执行任务这里测试大模型是否生效就选择Plan就行发句“你好”测试一下3、实现MCP服务首先创建一个SpringBoot项目注意最好使用JDK 17和SpringBoot 3引入以下依赖?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.4.4/versionrelativePath/!-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdai-demo/artifactIdversion0.0.1-SNAPSHOT/versionnameai-demo/namedescriptionai-demo/descriptionurl/licenseslicense//licensesdevelopersdeveloper//developersscmconnection/developerConnection/tag/url//scmpropertiesjava.version17/java.versionspring-ai.version1.0.0-M6/spring-ai.version/propertiesdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-mcp-server-spring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.36/version/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project其中最重要的一个依赖是spring-ai-mcp-server-spring-boot-starter引入这个依赖后项目才能和大模型进行通信之后编写服务类实现文件写入的具体逻辑importcom.example.aidemo.bean.FileWriteRequest;importlombok.extern.slf4j.Slf4j;importorg.springframework.ai.tool.annotation.Tool;importorg.springframework.stereotype.Service;importjava.nio.file.Path;importjava.nio.file.Paths;Slf4jServicepublicclassLocalFileService{Tool(description写入本地文件自动创建文件和目录)publicStringwriteLocalFile(FileWriteRequestrequest){StringfilePathrequest.getFilePath();PathpathPaths.get(filePath);PathpathParentpath.getParent();Stringcontentrequest.getContent();// 文件内容try{if(!pathParent.toFile().exists()){pathParent.toFile().mkdirs();}java.nio.file.Files.write(path,content.getBytes());return写入成功;}catch(Exceptione){log.error(写入文件失败,e);return写入文件失败;}}}importcom.fasterxml.jackson.annotation.JsonInclude;importcom.fasterxml.jackson.annotation.JsonProperty;importcom.fasterxml.jackson.annotation.JsonPropertyDescription;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;NoArgsConstructorAllArgsConstructorDataJsonInclude(JsonInclude.Include.NON_NULL)publicclassFileWriteRequest{JsonProperty(requiredtrue,valuefilePath)JsonPropertyDescription(需要写入的文件路径)privateStringfilePath;JsonProperty(requiredtrue,valuecontent)JsonPropertyDescription(写入文件的具体内容)privateStringcontent;}将服务类注册进MCP服务importcom.example.aidemo.service.LocalFileService;importorg.springframework.ai.tool.ToolCallbackProvider;importorg.springframework.ai.tool.method.MethodToolCallbackProvider;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassMcpServerConfig{BeanpublicToolCallbackProviderlocalFileToolCallbackProvider(LocalFileServicelocalFileService){returnMethodToolCallbackProvider.builder().toolObjects(localFileService).build();}}之后可以启动该项目查看日志在日志里可以看到已经成功注册了一个工具就是之前编写的文件写入服务。然后将该项目打成jar包就行了4、在Cline中配置MCP服务在Cline的右上方点击MCP Server再点击Installed再点击下面的Configure MCP Servers配置文件其他的基本都是固定的需要改变的是jar包的位置和方法名如果方法涉及到中文输入比如查询某个城市的天气需要在启动命令中添加-Dfile.encodingUTF-8 -Dconsole.encodingUTF-8配置完毕保存后Cline会自动更新配置更新完毕后就可以在Installed一栏中看到本地的MCP服务了至此已全部完成之后测试就行测试返回Cline对话框选择Act模式之后向大模型交代任务大模型会根据语义理解并自动调用本地配置好的MCP服务
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

自已怎样网站别乱买,3步搞定性能优化与备案 2026/9/27 8:22:15

自已怎样网站别乱买,3步搞定性能优化与备案

自已怎样网站别乱买,3步搞定性能优化与备案 别再被那些“一键生成”的模板网站骗了。看着界面花里胡哨,打开慢得像蜗牛,改个颜色都要找客服排队三天,这种“模板网站太丑不够用”的痛点,多少创业团队负责人都踩过坑。 更糟心的是,模板站往往在…

阅读更多 →
48 小时极客原型:用 Viem 与 Framer Motion 打造全链 Web3 多签金库联签模拟器 2026/9/27 8:22:15

48 小时极客原型:用 Viem 与 Framer Motion 打造全链 Web3 多签金库联签模拟器

48 小时极客原型:用 Viem 与 Framer Motion 打造全链 Web3 多签金库联签模拟器在 DAO 国库治理与企业级 Web3 资产管理中,多签金库(Multi-signature Vault / Gnosis Safe 2-of-3 / 3-of-5) 是保障数亿美元资金安全的终极防御墙。 …

阅读更多 →
Microsoft AI Lab 仓库导览:体验、学习与编码微软 AI 最新创新 2026/9/27 8:22:15

Microsoft AI Lab 仓库导览:体验、学习与编码微软 AI 最新创新

示例工程 【免费下载链接】ailab Experience, Learn and Code the latest breakthrough innovations with Microsoft AI 项目地址: https://gitcode.com/gh_mirrors/ai/ailab 点击查看 免费下载 Microsoft AI Lab 是微软面向开发者社区推出的 AI 实验开源项目&…

阅读更多 →
物理机与虚拟化资源碎片整理与容量再平衡 2026/9/27 8:22:15

物理机与虚拟化资源碎片整理与容量再平衡

物理机与虚拟化资源碎片整理与容量再平衡在支撑大促的超大规模底层算力机群(如 2,000 台 128 核 512GB 内存的高规格裸金属物理机)长期运维中,存在着一个吞噬企业巨额算力资本的“隐形黑洞”——“计算资源碎片化与被搁浅的死算力&#xff08…

阅读更多 →
palera1n checkm8 越狱完整实操指南:A8–A11 设备在 iOS 15+ 上从零到 root 环境一文搞定 2026/9/27 8:22:15

palera1n checkm8 越狱完整实操指南:A8–A11 设备在 iOS 15+ 上从零到 root 环境一文搞定

palera1n checkm8 越狱完整实操指南:A8–A11 设备在 iOS 15 上从零到 root 环境一文搞定 【免费下载链接】palera1n Jailbreak for A8 through A11, T2 devices, on iOS/iPadOS/tvOS 15.0, bridgeOS 5.0 and higher. 项目地址: https://gitcode.com/GitHub_Trendi…

阅读更多 →
商业化定价战术(四):阶梯式返点与代理商分销渠道激励机制设计实战 2026/9/27 8:22:09

商业化定价战术(四):阶梯式返点与代理商分销渠道激励机制设计实战

商业化定价战术(四):阶梯式返点与代理商分销渠道激励机制设计实战在企业级软件与 AI 中台的全国性市场扩张中,依靠初创公司内部十来个人的直销团队(Direct Sales),无论是人力覆盖半径还是行业深…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉