1. MCP服务资源类型

1.1 Resource

@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"

官方解释:
Expose data through Resources (think of these sort of like GET endpoints; they are used to load information into the LLM’s context)

我们可以通过@mcp.resource这个装饰器来暴露数据,并将其作为一个信息载入到LLM的上下文中,一般体现为一个JSON数据

1.2 Tools

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

官方解释:
Provide functionality through Tools (sort of like POST endpoints; they are used to execute code or otherwise produce a side effect)

我们可以通过@mcp.tool来定义一个mcp工具,tool通过被用来执行一段代码。为了让大模型能够理解这个函数的意义,我们需要添加注释,在上述代码中的示例就是"Add two numbers"

1.3 Prompt

@mcp.prompt()
def greet_user(name: str, style: str = "friendly") -> str:
    """Generate a greeting prompt"""
    styles = {
        "friendly": "Please write a warm, friendly greeting",
        "formal": "Please write a formal, professional greeting",
        "casual": "Please write a casual, relaxed greeting",
    }
 
    return f"{styles.get(style, styles['friendly'])} for someone named {name}."

官方解释:
Define interaction patterns through Prompts (reusable templates for LLM interactions)

我们可以通过@mcp.prompt来进行一个提示词的设计,一个好的提示词可以帮助我们更好的完成任务。例如你正在学习go语言,那么你肯定希望LLM给你的代码是go语言相关的,因此你可以设定相关的提示词,例如:“你是一名精通go语言的教师,每次回答问题都需要使用go语言输出对应的代码,并且做出详细的解释”

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐