1. 引言:AI模型为何需要“记忆”能力?
1.1 上下文是AI理解任务的基础
AI模型在执行任务时,往往需要理解上下文信息,例如:
- 在代码补全中,模型需要知道当前函数名、参数类型、变量作用域;
- 在对话系统中,模型需要记住用户之前的意图和历史问题;
- 在图像理解中,模型需要结合上下文描述生成更准确的输出。
然而,传统模型调用方式通常将上下文信息以一次性Prompt形式传递,导致模型“健忘”,无法维持连贯性。
1.2 模型“健忘”的代价与局限
示例:传统方式下的代码补全
prompt = """
你是一个Java代码助手。请补全以下函数:
public int calculateSum(int a, int b) {
"""
response = model.generate(prompt)
print(response)
输出可能为:
return a + b;
}
这种方式虽然能完成任务,但存在以下问题:
- 无法维护函数状态:如果用户继续编写
calculateProduct
,模型无法知道这是同一个类中的另一个函数。 - 重复传递成本高:每次调用都要重复传入完整上下文。
- 逻辑断裂:多轮交互中容易丢失上下文,导致输出不一致。
1.3 MCP协议的提出:让上下文可标识、可继承、可共享
为了解决这些问题,我们提出了 MCP(Model Context Protocol) —— 一种用于管理和传递上下文信息的协议,让AI模型具备“记忆”能力。
MCP 的核心特性包括:
- 上下文标识:每个上下文片段都有唯一 ID(
contextId
)。 - 上下文继承:新请求可以基于已有上下文构建。
- 上下文共享:多个模型或服务之间共享上下文状态。
- 上下文生命周期管理:支持创建、更新、查询、销毁上下文。
2. 上下文缺失带来的挑战
2.1 单次调用模型的局限性
2.1.1 Prompt重复传递的效率问题
在传统方式中,每次调用都要传递完整的上下文内容,效率低下。例如:
prompt = """
你是一个Java代码助手。用户正在编写一个类 Calculator,请根据上下文补全代码:
public class Calculator {
public int calculateSum(int a, int b) {
return a + b;
}
public int calculateProduct(int a, int b) {
"""
response = model.generate(prompt)
print(response)
这会导致:
- 冗余内容传输:
calculateSum
函数的内容每次都重复传入。 - 模型负担加重:模型需要处理大量重复信息,影响响应速度和准确性。
使用MCP优化上下文传递
我们可以使用 MCP 来避免重复传递:
# 创建上下文
ctx1 = createContext("public class Calculator {\n public int calculateSum(int a, int b) {\n return a + b;\n }")
# 基于已有上下文创建新上下文
ctx2 = createContext(" public int calculateProduct(int a, int b) {", parentId=ctx1["contextId"])
# 模型调用时只需传入 contextId
suggestion = model.suggest(ctx2["contextId"])
print(suggestion)
输出:
return a * b;
}
这样模型就能基于历史上下文进行补全,无需重复传入全部内容。
2.1.2 多轮交互中的上下文断裂
在对话系统中,传统方式下模型无法记住之前的对话内容:
response1 = model.generate("用户:你好,请问你能帮我做什么?")
print(response1)
# 输出:我可以帮你写代码、回答问题、解释代码等。
response2 = model.generate("用户:我想了解天气情况。")
print(response2)
# 输出:抱歉,我无法提供天气信息。
但如果使用 MCP:
ctx1 = createContext("用户:你好,请问你能帮我做什么?")
response1 = model.respond(ctx1["contextId"])
print(response1)
ctx2 = createContext("用户:我想了解天气情况。", parentId=ctx1["contextId"])
response2 = model.respond(ctx2["contextId"])
print(response2)
模型就能理解上下文关系,避免重复解释。
2.2 代码场景下的上下文丢失
2.2.1 函数级理解的缺失
传统方式下,模型无法理解当前函数的作用域和上下文:
prompt = """
public class Main {
public static void main(String[] args) {
int result = calculateSum(2, 3);
System.out.println(result);
}
public static int calculateSum(int a, int b) {
return a + b;
}
public static int calculateDifference(int a, int b) {
"""
response = model.generate(prompt)
print(response)
输出可能为:
return a - b;
}
但模型并不知道这是 calculateDifference
函数的一部分,也无法理解它与 calculateSum
的关系。
使用MCP增强函数级理解
# 创建上下文表示当前文件
ctx_file = createContext("public class Main {")
# 创建 main 函数上下文
ctx_main = createContext(" public static void main(String[] args) {\n int result = calculateSum(2, 3);\n System.out.println(result);\n }", parentId=ctx_file["contextId"])
# 创建 calculateSum 函数上下文
ctx_sum = createContext(" public static int calculateSum(int a, int b) {\n return a + b;\n }", parentId=ctx_file["contextId"])
# 创建 calculateDifference 函数上下文
ctx_diff = createContext(" public static int calculateDifference(int a, int b) {", parentId=ctx_file["contextId"])
# 模型调用时传入上下文ID
suggestion = model.suggest(ctx_diff["contextId"])
print(s