当前位置: 首页 > wzjs >正文

wordpress搭建子網站扶余手机网站开发

wordpress搭建子網站,扶余手机网站开发,盈利网站备案,qq小程序开放平台目录 代码代码解释导入模块常量定义MCP服务器初始化工具函数定义1. search_papers 函数2. extract_info 函数 主程序总结 运行示例 代码 import arxiv import json import os from typing import List from mcp.server.fastmcp import FastMCPPAPER_DIR "papers"mc…

目录

    • 代码
    • 代码解释
      • 导入模块
      • 常量定义
      • MCP服务器初始化
      • 工具函数定义
        • 1. search_papers 函数
        • 2. extract_info 函数
      • 主程序
      • 总结
    • 运行示例

代码

import arxiv
import json
import os
from typing import List
from mcp.server.fastmcp import FastMCPPAPER_DIR = "papers"mcp = FastMCP("research")@mcp.tool()
def search_papers(topic: str, max_results: int = 5) -> List[str]:"""Search for papers on arXiv based on a topic and store their information.Args:topic: The topic to search formax_results: Maximum number of results to retrieve (default: 5)Returns:List of paper IDs found in the search"""# Use arxiv to find the papersclient = arxiv.Client()# Search for the most relevant articles matching the queried topicsearch = arxiv.Search(query = topic,max_results = max_results,sort_by = arxiv.SortCriterion.Relevance)papers = client.results(search)# Create directory for this topicpath = os.path.join(PAPER_DIR, topic.lower().replace(" ", "_"))os.makedirs(path, exist_ok=True)file_path = os.path.join(path, "papers_info.json")# Try to load existing papers infotry:with open(file_path, "r") as json_file:papers_info = json.load(json_file)except (FileNotFoundError, json.JSONDecodeError):papers_info = {}# Process each paper and add to papers_infopaper_ids = []for paper in papers:paper_ids.append(paper.get_short_id())paper_info = {'title': paper.title,'authors': [author.name for author in paper.authors],'summary': paper.summary,'pdf_url': paper.pdf_url,'published': str(paper.published.date())}papers_info[paper.get_short_id()] = paper_info# Save updated papers_info to json filewith open(file_path, "w") as json_file:json.dump(papers_info, json_file, indent=2)print(f"Results are saved in: {file_path}")return paper_ids@mcp.tool()
def extract_info(paper_id: str) -> str:"""Search for information about a specific paper across all topic directories.Args:paper_id: The ID of the paper to look forReturns:JSON string with paper information if found, error message if not found"""for item in os.listdir(PAPER_DIR):item_path = os.path.join(PAPER_DIR, item)if os.path.isdir(item_path):file_path = os.path.join(item_path, "papers_info.json")if os.path.isfile(file_path):try:with open(file_path, "r") as json_file:papers_info = json.load(json_file)if paper_id in papers_info:return json.dumps(papers_info[paper_id], indent=2)except (FileNotFoundError, json.JSONDecodeError) as e:print(f"Error reading {file_path}: {str(e)}")continuereturn f"There's no saved information related to paper {paper_id}."if __name__ == "__main__":mcp.run(transport="stdio")

代码解释

导入模块

import arxiv        # 用于访问arXiv API搜索论文
import json         # 处理JSON数据
import os           # 操作系统功能,如文件路径处理
from typing import List  # 类型提示
from mcp.server.fastmcp import FastMCP  # 导入MCP框架

常量定义

PAPER_DIR = "papers"  # 定义存储论文信息的目录

MCP服务器初始化

mcp = FastMCP("research")  # 创建一个名为"research"的MCP服务器实例

工具函数定义

1. search_papers 函数
@mcp.tool()
def search_papers(topic: str, max_results: int = 5) -> List[str]:

这个函数被注册为MCP工具,用于在arXiv上搜索特定主题的论文并保存信息:

  • 装饰器@mcp.tool() 将此函数注册为MCP服务的工具
  • 参数
    • topic: 要搜索的主题
    • max_results: 最大结果数量(默认5个)
  • 返回值:找到的论文ID列表

功能流程

  1. 创建arXiv客户端
  2. 按相关性搜索主题相关论文
  3. 为该主题创建目录(如papers/machine_learning
  4. 尝试加载已有的论文信息(如果存在)
  5. 处理每篇论文,提取标题、作者、摘要等信息
  6. 将论文信息保存到JSON文件中
  7. 返回论文ID列表
2. extract_info 函数
@mcp.tool()
def extract_info(paper_id: str) -> str:

这个函数也被注册为MCP工具,用于在所有主题目录中搜索特定论文的信息:

  • 装饰器@mcp.tool() 将此函数注册为MCP服务的工具
  • 参数paper_id - 要查找的论文ID
  • 返回值:包含论文信息的JSON字符串(如果找到),否则返回错误信息

功能流程

  1. 遍历papers目录下的所有子目录
  2. 在每个子目录中查找papers_info.json文件
  3. 如果找到文件,检查是否包含指定的论文ID
  4. 如果找到论文信息,返回格式化的JSON字符串
  5. 如果未找到,返回未找到的提示信息

主程序

if __name__ == "__main__":mcp.run(transport="stdio")

总结

research_server.py是一个基于MCP框架的研究服务器,提供了两个主要工具:

  1. 搜索arXiv上的论文并保存信息
  2. 提取已保存的论文信息

这个服务器可以作为AI助手的后端,通过MCP协议与前端交互,提供论文研究相关的功能。

运行示例

可用inspector工具查看,可以参照这个例子MarkItDown-MCP 测试与debug

请添加图片描述
请添加图片描述

前一节链接:
吴恩达MCP课程(1):chat_bot


文章转载自:

http://mhcK9qyc.xfcjs.cn
http://v1um0L1u.xfcjs.cn
http://cSnY4HuF.xfcjs.cn
http://wsxArgTb.xfcjs.cn
http://mSJlHvon.xfcjs.cn
http://5fC4Ax56.xfcjs.cn
http://a6nlusrA.xfcjs.cn
http://0bz164ez.xfcjs.cn
http://5UJx7QLN.xfcjs.cn
http://enVTK2Mg.xfcjs.cn
http://QwHsj4Hk.xfcjs.cn
http://TWaZHj6D.xfcjs.cn
http://FPkHvLqk.xfcjs.cn
http://1tNsVger.xfcjs.cn
http://eByyDytH.xfcjs.cn
http://KhRSaYpi.xfcjs.cn
http://MbDxeouD.xfcjs.cn
http://yvvAqPLF.xfcjs.cn
http://yWJEUbUg.xfcjs.cn
http://r13z8B17.xfcjs.cn
http://jEqQ8HGW.xfcjs.cn
http://TbdpHr8x.xfcjs.cn
http://sSvRdqnj.xfcjs.cn
http://PP5IG012.xfcjs.cn
http://TAuoFbsn.xfcjs.cn
http://NZHvJwG1.xfcjs.cn
http://LDYxoUSx.xfcjs.cn
http://4oj3nkLh.xfcjs.cn
http://m4ZP2Mb2.xfcjs.cn
http://TWpQRXXl.xfcjs.cn
http://www.dtcms.com/wzjs/777231.html

相关文章:

  • 建设一个网站需要学哪些做的网站有营销效果吗
  • 手机网站什么意思wordpress非插件文章浏览量
  • 能源网站建设wordpress 文章 页码
  • 网站导航二级菜单怎么做出来的中英文对照网站怎么做
  • 北京做网站推广一个月多少钱网站建设中的功能模块描述
  • 58同城网站建设推广排名做可视化图表的网站
  • 网站建设项目怎么跟进客户报价单模板
  • 网站开发的微端湖北建设银行官方网站首页
  • 模板网站有利于优化网站申请域名流程
  • 网站平台搭建吾爱源码网
  • 郎溪做网站静安广州网站建设
  • 公司网站建设p开发wordpress多人博客
  • 字体设计素材网seo刷排名公司
  • 做pc端网站包括哪些wordpress后台菜单
  • 怎么做网盘网站以下属于免费推广的方式的是
  • 雪锐琴网站建设企业做网站有什么用
  • phpcms中英文网站模板泸州 网站建设
  • ae模板免费网站免版权费自建网站
  • 黄骅港在哪个省宁波关键词在线优化
  • 网站文章超链接怎么做常见网页设计
  • 网站后台统计代码wordpress 相册 插件
  • 轻应用网站模板wordpress .mo .po
  • 杨凌住房和城乡建设局网站wordpress后台文章自定义字段面板
  • 深圳品牌营销网站共享办公室租赁平台
  • 做网站准备什么软件ip地址反查域名
  • 网站导航仿站福州做网站哪家公司好
  • 网站策划方案书的内容郑州网站制作汉狮
  • 网站如何建设短网址还原网站
  • 广元市城乡规划建设监察大队网站制作网页网站
  • wap网站搜索接兼职建设网站