-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support MCP #2665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Support MCP #2665
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# coding=utf-8 | ||
|
||
from .impl import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# coding=utf-8 | ||
|
||
from typing import Type | ||
|
||
from rest_framework import serializers | ||
|
||
from application.flow.i_step_node import INode, NodeResult | ||
from common.util.field_message import ErrMessage | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class McpNodeSerializer(serializers.Serializer): | ||
mcp_servers = serializers.JSONField(required=True, | ||
error_messages=ErrMessage.char(_("Mcp servers"))) | ||
|
||
mcp_server = serializers.CharField(required=True, | ||
error_messages=ErrMessage.char(_("Mcp server"))) | ||
|
||
mcp_tool = serializers.CharField(required=True, error_messages=ErrMessage.char(_("Mcp tool"))) | ||
|
||
tool_params = serializers.DictField(required=True, | ||
error_messages=ErrMessage.char(_("Tool parameters"))) | ||
|
||
|
||
class IMcpNode(INode): | ||
type = 'mcp-node' | ||
|
||
def get_node_params_serializer_class(self) -> Type[serializers.Serializer]: | ||
return McpNodeSerializer | ||
|
||
def _run(self): | ||
return self.execute(**self.node_params_serializer.data, **self.flow_params_serializer.data) | ||
|
||
def execute(self, mcp_servers, mcp_server, mcp_tool, tool_params, **kwargs) -> NodeResult: | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# coding=utf-8 | ||
|
||
from .base_mcp_node import BaseMcpNode |
56 changes: 56 additions & 0 deletions
56
apps/application/flow/step_node/mcp_node/impl/base_mcp_node.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# coding=utf-8 | ||
import asyncio | ||
import json | ||
from typing import List | ||
|
||
from langchain_mcp_adapters.client import MultiServerMCPClient | ||
|
||
from application.flow.i_step_node import NodeResult | ||
from application.flow.step_node.mcp_node.i_mcp_node import IMcpNode | ||
|
||
|
||
class BaseMcpNode(IMcpNode): | ||
def save_context(self, details, workflow_manage): | ||
self.context['result'] = details.get('result') | ||
self.context['tool_params'] = details.get('tool_params') | ||
self.context['mcp_tool'] = details.get('mcp_tool') | ||
self.answer_text = details.get('result') | ||
|
||
def execute(self, mcp_servers, mcp_server, mcp_tool, tool_params, **kwargs) -> NodeResult: | ||
servers = json.loads(mcp_servers) | ||
params = self.handle_variables(tool_params) | ||
|
||
async def call_tool(s, session, t, a): | ||
async with MultiServerMCPClient(s) as client: | ||
s = await client.sessions[session].call_tool(t, a) | ||
return s | ||
|
||
res = asyncio.run(call_tool(servers, mcp_server, mcp_tool, params)) | ||
return NodeResult({'result': [content.text for content in res.content], 'tool_params': params, 'mcp_tool': mcp_tool}, {}) | ||
|
||
def handle_variables(self, tool_params): | ||
# 处理参数中的变量 | ||
for k, v in tool_params.items(): | ||
if type(v) == str: | ||
tool_params[k] = self.workflow_manage.generate_prompt(tool_params[k]) | ||
if type(v) == dict: | ||
self.handle_variables(v) | ||
return tool_params | ||
|
||
def get_reference_content(self, fields: List[str]): | ||
return str(self.workflow_manage.get_reference_field( | ||
fields[0], | ||
fields[1:])) | ||
|
||
def get_details(self, index: int, **kwargs): | ||
return { | ||
'name': self.node.properties.get('stepName'), | ||
"index": index, | ||
'run_time': self.context.get('run_time'), | ||
'status': self.status, | ||
'err_message': self.err_message, | ||
'type': self.node.type, | ||
'mcp_tool': self.context.get('mcp_tool'), | ||
'tool_params': self.context.get('tool_params'), | ||
'result': self.context.get('result'), | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your updated code includes several improvements and additions:
Concurrency Improvements:
mcp_response_generator
function now uses asynchronous programming to efficiently fetch responses from multi-server Message Collaboration Platform (MCP) servers, utilizingasyncio
._yield_mcp_response
coroutine to handle streaming of AI messages.Efficiency Enhancements:
BaseMessage
,AIMessage
) that were already included fromlangchain_core.messages
.Error Handling:
__init__
method and within the coroutine functions (_yield_mcp_response, _get_mcp_response).new_event_loop()
and closing it after use.API Enhancements:
mcp_response_generator
_get_mcp_response
)Functionality Extensions:
mcp_enable
and providing MCP server configurations viamcp_servers
.Potential Considerations
These changes make your code more robust, efficient, and capable of handling concurrent interactions well with MCPServers.