流利的MCP
一种用于构建具有智能推理能力的模型上下文协议(MCP)服务器的现代框架。

概述
Fluent MCP是一个用于搭建和管理MCP服务器的工具包,重点是人工智能集成。它提供了一种结构化的方法来构建服务器,这些服务器可以使用语言模型执行嵌入式推理,注册和执行工具,并管理提示和配置。
该框架设计为可扩展的,允许LLM构建和注册自己的工具,支持自我改进的AI系统的开发。
核心架构模式
Fluent MCP实现了一种强大的架构模式,从根本上改变了人工智能系统的交互方式:
两层LLM架构
- 嵌入式LLM:执行复杂推理和多步骤任务的内部LLM
- 消费LLM:与MCP服务器交互的外部LLM(如Claude)
工具分离
- 嵌入式工具:仅适用于嵌入式LLM的内部工具,不暴露在外部
- 外部工具:通过MCP协议暴露于消费LLM的工具
推理卸载
- 复杂的多步推理从消费LLM卸载到嵌入式LLM
- 外部工具可以在内部利用嵌入式推理,同时呈现一个简单的界面
- 这创建了一个“推理三明治”,其中复杂的逻辑发生在中间层
益处
- 代币效率:消费LLM通过将推理卸载到嵌入式LLM来使用更少的令牌
- 降低成本:较小的专用模型可以以较低的成本处理特定的推理任务
- 复杂性隐藏:复杂的多步骤流程隐藏在简单的界面后面
- 关注点分离:明确暴露的内容和内部内容之间的界限
特性
- 推理卸载:将复杂的推理从消费LLM转移到嵌入式LLM,以提高令牌和成本效率
- 工具分离:嵌入式工具(内部)和外部工具(外露)之间的明确区别
- 服务器支架:生成具有适当结构的新MCP服务器项目
- LLM集成:无缝连接到不同提供商的语言模型
- 工具注册表:注册嵌入式工具(内部使用)和外部工具(暴露于消耗LLM)
- 嵌入式推理:使用LLM进行推理并执行其工具调用
- 及时管理:从文件加载和管理提示,支持frontmatter中的工具定义
- 错误处理:LLM集成和工具执行的强大错误处理
安装
pip install fluent_mcp发展:
# Clone the repository
git clone https://github.com/yourusername/fluent_mcp.git
cd fluent_mcp
# Install in development mode with extra dependencies
pip install -e ".[dev]"快速开始
创建新服务器
使用CLI搭建新服务器:
fluent-mcp new my_server或者以编程方式创建服务器:
from fluent_mcp import scaffold_server
scaffold_server(
output_dir=".",
server_name="my_server",
description="My MCP server with AI capabilities"
)实现核心架构模式
from fluent_mcp.core.tool_registry import register_embedded_tool, register_external_tool
from fluent_mcp.core.llm_client import run_embedded_reasoning
import asyncio
# 1. Define embedded tools (ONLY available to the embedded LLM)
@register_embedded_tool()
def search_database(query: str) -> list:
"""Search the database for information (only used internally)."""
# Implementation...
return ["result1", "result2"]
@register_embedded_tool()
def analyze_data(data: list) -> dict:
"""Analyze data and extract insights (only used internally)."""
# Implementation...
return {"key_insight": "finding", "confidence": 0.95}
# 2. Define an external tool that leverages embedded reasoning
@register_external_tool()
async def research_question(question: str) -> dict:
"""
Research a question and provide a comprehensive answer.
This external tool is exposed to consuming LLMs but internally
uses embedded reasoning with access to embedded tools.
"""
# Define system prompt for embedded reasoning
system_prompt = """
You are a research assistant with access to internal tools:
- search_database: Search for information
- analyze_data: Analyze and extract insights
Use these tools to thoroughly research the question.
"""
# Run embedded reasoning (this is where the magic happens)
result = await run_embedded_reasoning(
system_prompt=system_prompt,
user_prompt=f"Research this question: {question}"
)
# Return a clean, structured response to the consuming LLM
return {
"answer": result["content"],
"confidence": 0.9,
"sources": ["source1", "source2"]
}使用体系结构模式运行服务器
from fluent_mcp import create_mcp_server
from my_tools import search_database, analyze_data, research_question
# Create and run MCP server
server = create_mcp_server(
server_name="my_server",
# Embedded tools (ONLY available to the embedded LLM)
embedded_tools=[search_database, analyze_data],
# External tools (exposed to consuming LLMs)
external_tools=[research_question],
config={
"provider": "ollama",
"model": "llama2",
"base_url": "http://localhost:11434",
"api_key": "ollama"
}
)
server.run()使用带有工具定义的提示
Fluent MCP支持直接在提示的frontmatter中定义提示可用的工具:
---
name: math_tools
description: A prompt that uses math-related tools
model: gpt-4
temperature: 0.3
tools:
- add_numbers
- multiply_numbers
---
You are a math assistant that can perform calculations.
Use the available tools to help solve math problems.当将此提示与嵌入式推理一起使用时,只有指定的工具可用:
from fluent_mcp.core.llm_client import run_embedded_reasoning
# Get a prompt with tool definitions
math_prompt = server.get_prompt("math_tools")
# Run embedded reasoning with only the tools defined in the prompt
result = await run_embedded_reasoning(
system_prompt=math_prompt["template"],
user_prompt="What is 5 + 3?",
prompt=math_prompt # Pass the prompt to use its tool definitions
)这种方法允许更精确地控制哪些工具可用于不同的提示,从而提高安全性并减少意外使用工具的机会。
文档
有关更详细的文档,请参阅 文档目录:
例子
看看 示例目录 有关完整的工作示例:
发展
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run linting
flake8
black .
isort .许可证
麻省理工学院
