MCP: MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications. 本次案例目标是实际简单的问答命令,通过指令操作读取文档、修改文档
MCP (Model Context Protocol) 架构图
mcp client 会提供一系列工具例如
0 = {ToolSpecification@3357} "ToolSpecification { name = "read_file", description = "Read the complete contents of a file from the file system. Handles various text encodings and provides detailed error messages if the file cannot be read. Use this tool when you need to examine the contents of a single file. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }}, required = [path], additionalProperties = false, definitions = {} } }"
1 = {ToolSpecification@3351} "ToolSpecification { name = "read_multiple_files", description = "Read the contents of multiple files simultaneously. This is more efficient than reading files one by one when you need to analyze or compare multiple files. Each file's content is returned with its path as a reference. Failed reads for individual files won't stop the entire operation. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {paths=JsonArraySchema {description = null, items = JsonStringSchema {description = null } }}, required = [paths], additionalProperties = false, definitions = {} } }"
2 = {ToolSpecification@3345} "ToolSpecification { name = "write_file", description = "Create a new file or completely overwrite an existing file with new content. Use with caution as it will overwrite existing files without warning. Handles text content with proper encoding. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }, content=JsonStringSchema {description = null }}, required = [path, content], additionalProperties = false, definitions = {} } }"
3 = {ToolSpecification@3349} "ToolSpecification { name = "create_directory", description = "Create a new directory or ensure a directory exists. Can create multiple nested directories in one operation. If the directory already exists, this operation will succeed silently. Perfect for setting up directory structures for projects or ensuring required paths exist. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }}, required = [path], additionalProperties = false, definitions = {} } }"
4 = {ToolSpecification@3343} "ToolSpecification { name = "list_directory", description = "Get a detailed listing of all files and directories in a specified path. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is essential for understanding directory structure and finding specific files within a directory. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }}, required = [path], additionalProperties = false, definitions = {} } }"
5 = {ToolSpecification@3347} "ToolSpecification { name = "move_file", description = "Move or rename files and directories. Can move files between directories and rename them in a single operation. If the destination exists, the operation will fail. Works across different directories and can be used for simple renaming within the same directory. Both source and destination must be within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {source=JsonStringSchema {description = null }, destination=JsonStringSchema {description = null }}, required = [source, destination], additionalProperties = false, definitions = {} } }"
6 = {ToolSpecification@3353} "ToolSpecification { name = "search_files", description = "Recursively search for files and directories matching a pattern. Searches through all subdirectories from the starting path. The search is case-insensitive and matches partial names. Returns full paths to all matching items. Great for finding files when you don't know their exact location. Only searches within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }, pattern=JsonStringSchema {description = null }}, required = [path, pattern], additionalProperties = false, definitions = {} } }"
7 = {ToolSpecification@3359} "ToolSpecification { name = "get_file_info", description = "Retrieve detailed metadata about a file or directory. Returns comprehensive information including size, creation time, last modified time, permissions, and type. This tool is perfect for understanding file characteristics without reading the actual content. Only works within allowed directories.", parameters = JsonObjectSchema {description = null, properties = {path=JsonStringSchema {description = null }}, required = [path], additionalProperties = false, definitions = {} } }"
8 = {ToolSpecification@3355} "ToolSpecification { name = "list_allowed_directories", description = "Returns the list of directories that this server is allowed to access. Use this to understand which directories are available before trying to access files.", parameters = JsonObjectSchema {description = null, properties = {}, required = [], additionalProperties = null, definitions = {} } }"
ToolExecutor. 提供了具体的执行命令。
最终构造的promopt即为命令的rounter,最终交给llm分析的请求 api-docs.deepseek.com/zh-cn/api/c… tools参数 模型可能会调用的 tool 的列表。
openAI request sample
ChatCompletionRequest{model=deepseek-chat, messages=[UserMessage{role=USER, content=Read the contents of the file /file.txt, name=null}], temperature=null, topP=null, n=null, stream=null, streamOptions=null, stop=[], maxTokens=null, maxCompletionTokens=null, presencePenalty=null, frequencyPenalty=null, logitBias={}, user=null, responseFormat=null, seed=null, tools=[Tool{type=FUNCTION, function=Function{name=search_files, description=Recursively search for files and directories matching a pattern. Searches through all subdirectories from the starting path. The search is case-insensitive and matches partial names. Returns full paths to all matching items. Great for finding files when you don't know their exact location. Only searches within allowed directories., strict=null, parameters={type=object, properties={path={type=string}, pattern={type=string}}, required=[path, pattern]}}}, Tool{type=FUNCTION, function=Function{name=list_directory, description=Get a detailed listing of all files and directories in a specified path. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is essential for understanding directory structure and finding specific files within a directory. Only works within allowed directories., strict=null, parameters={type=object, properties={path={type=string}}, required=[path]}}}, Tool{type=FUNCTION, function=Function{name=read_multiple_files, description=Read the contents of multiple files simultaneously. This is more efficient than reading files one by one when you need to analyze or compare multiple files. Each file's content is returned with its path as a reference. Failed reads for individual files won't stop the entire operation. Only works within allowed directories., strict=null, parameters={type=object, properties={paths={type=array, items={type=string}}}, required=[paths]}}}, Tool{type=FUNCTION, function=Function{name=create_directory, description=Create a new directory or ensure a directory exists. Can create multiple nested directories in one operation. If the directory already exists, this operation will succeed silently. Perfect for setting up directory structures for projects or ensuring required paths exist. Only works within allowed directories., strict=null, parameters={type=object, properties={path={type=string}}, required=[path]}}}, Tool{type=FUNCTION, function=Function{name=list_allowed_directories, description=Returns the list of directories that this server is allowed to access. Use this to understand which directories are available before trying to access files., strict=null, parameters={type=object, properties={}, required=[]}}}, Tool{type=FUNCTION, function=Function{name=get_file_info, description=Retrieve detailed metadata about a file or directory. Returns comprehensive information including size, creation time, last modified time, permissions, and type. This tool is perfect for understanding file characteristics without reading the actual content. Only works within allowed directories., strict=null, parameters={type=object, properties={path={type=string}}, required=[path]}}}, Tool{type=FUNCTION, function=Function{name=move_file, description=Move or rename files and directories. Can move files between directories and rename them in a single operation. If the destination exists, the operation will fail. Works across different directories and can be used for simple renaming within the same directory. Both source and destination must be within allowed directories., strict=null, parameters={type=object, properties={source={type=string}, destination={type=string}}, required=[source, destination]}}}, Tool{type=FUNCTION, function=Function{name=read_file, description=Read the complete contents of a file from the file system. Handles various text encodings and provides detailed error messages if the file cannot be read. Use this tool when you need to examine the contents of a single file. Only works within allowed directories., strict=null, parameters={type=object, properties={path={type=string}}, required=[path]}}}, Tool{type=FUNCTION, function=Function{name=write_file, description=Create a new file or completely overwrite an existing file with new content. Use with caution as it will overwrite existing files without warning. Handles text content with proper encoding. Only works within allowed directories., strict=null, parameters={type=object, properties={path={type=string}, content={type=string}}, required=[path, content]}}}], toolChoice=null, parallelToolCalls=null, store=null, metadata={}, reasoningEffort=null, serviceTier=null, functions=null, functionCall=null}
openAI response sample
ChatCompletionResponse{id=af7c1ded-c562-48f3-b843-f4c6fc189a93, created=1753695038, model=deepseek-chat, choices=[ChatCompletionChoice{index=0, message=AssistantMessage{role=ASSISTANT, content=, name=null, toolCalls=[ToolCall{id=call_0_bccaa613-f02b-43bc-850c-c3eb0df0c91b, index=0, type=FUNCTION, function=FunctionCall{name=read_file, arguments={"path":"/file.txt"}}}], refusal=null, functionCall=null}, delta=null, finishReason=tool_calls}], usage=Usage{totalTokens=1237, promptTokens=1199, promptTokensDetails=PromptTokensDetails{cachedTokens=0}, completionTokens=38, completionTokensDetails=null}, systemFingerprint=fp_8802369eaa_prod0623_fp8_kvcache, serviceTier=null}
下一步为toolExecution 注意工具的执行本身也会被llm 会将结果交过llm进行理解
ToolMessage{role=TOOL, toolCallId=call_0_7fcc86ec-3257-42d8-97f4-ff20b4b15cb8, content=Kaboom!
2024-12-26 11:01:59 PST
2024-12-26 11:48:32 PST}
本身也可以进行进一步的调用。有点像是远程调用的过程,是 llm-> mcp-agent(me)
graph TB
subgraph "Client Side (LangChain4j Application)"
A[Chat Application] --> B[AiServices Builder]
B --> C[Bot Interface]
C --> D[ChatModel - DeepSeek/OpenAI]
C --> E[ToolProvider - McpToolProvider]
E --> F[McpClient - DefaultMcpClient]
end
subgraph "Transport Layer"
F --> G{Transport Type}
G -->|HTTP/SSE| H[HttpMcpTransport]
G -->|stdio| I[StdioMcpTransport]
end
subgraph "MCP Server Side"
H --> J[HTTP MCP Server<br/>localhost:3001/sse]
I --> K[Filesystem MCP Server<br/>npm subprocess]
J --> L[server-everything<br/>- add tool<br/>- calculation functions]
K --> M[server-filesystem<br/>- read_file tool<br/>- write_file tool<br/>- list_directory tool]
end
subgraph "Resources & Tools"
L --> N[Mathematical Operations<br/>Addition, etc.]
M --> O[Local File System<br/>src/main/resources/]
end
subgraph "Communication Flow"
P[User Query] --> A
A --> Q[Tool Call via MCP]
Q --> R[Tool Execution on Server]
R --> S[Result Return]
S --> T[AI Response to User]
end
style A fill:#e1f5fe
style D fill:#f3e5f5
style E fill:#e8f5e8
style J fill:#fff3e0
style K fill:#fff3e0
style L fill:#fce4ec
style M fill:#fce4ec
数据流向图
sequenceDiagram
participant User
participant Bot
participant ChatModel
participant McpClient
participant Transport
participant McpServer
participant Tools
User->>Bot: "What is 5+12?"
Bot->>ChatModel: Process query
ChatModel->>Bot: Need to use add tool
Bot->>McpClient: Request tool execution
McpClient->>Transport: Send MCP request
alt HTTP Transport
Transport->>McpServer: HTTP/SSE request
else stdio Transport
Transport->>McpServer: stdin/stdout communication
end
McpServer->>Tools: Execute add(5, 12)
Tools->>McpServer: Return result: 17
McpServer->>Transport: MCP response
Transport->>McpClient: Tool result
McpClient->>Bot: Tool execution result
Bot->>ChatModel: Process result
ChatModel->>Bot: Generate response
Bot->>User: "The result is 17"