xintongsong commented on code in PR #107: URL: https://github.com/apache/flink-agents/pull/107#discussion_r2269601859
########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): Review Comment: I think this should extends either `BaseTool` or `ToolMetadata`? ########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): + """MCP tool definition returned by list_tools or get_tool_definition.""" + + name: str + description: str + parameters: Dict[str, Any] = Field(default_factory=dict) + + def to_model_tool(self, chat_model: BaseChatModel) -> Dict[str, Any]: + """Convert this MCP tool definition to a model-specific tool dict. + + Default implementation is not provided; subclasses must override. + """ + raise NotImplementedError( + "to_model_tool must be implemented by MCPToolDefinition subclasses" + ) + + +class MCPPrompt(BaseModel): Review Comment: Should this extends `Prompt`? ########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): + """MCP tool definition returned by list_tools or get_tool_definition.""" + + name: str + description: str + parameters: Dict[str, Any] = Field(default_factory=dict) + + def to_model_tool(self, chat_model: BaseChatModel) -> Dict[str, Any]: + """Convert this MCP tool definition to a model-specific tool dict. + + Default implementation is not provided; subclasses must override. + """ + raise NotImplementedError( + "to_model_tool must be implemented by MCPToolDefinition subclasses" + ) + + +class MCPPrompt(BaseModel): + """MCP prompt definition returned by list_prompts/get_prompt.""" + + name: str + description: Optional[str] = None + arguments: Dict[str, Any] = Field(default_factory=dict) + + +class MCPServer(BaseTool, ABC): Review Comment: I think this should extend `Resource`, rather than `BaseTool`. ########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): + """MCP tool definition returned by list_tools or get_tool_definition.""" + + name: str + description: str + parameters: Dict[str, Any] = Field(default_factory=dict) + + def to_model_tool(self, chat_model: BaseChatModel) -> Dict[str, Any]: + """Convert this MCP tool definition to a model-specific tool dict. Review Comment: Does this mean a tool implementation needs to understand how to convert itself into a model-specific form for all supported chat models? I though it should be responsibility of the chat model implementations to convert the unified representation of mcp tools to what ever forms they need. ########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): + """MCP tool definition returned by list_tools or get_tool_definition.""" + + name: str + description: str + parameters: Dict[str, Any] = Field(default_factory=dict) + + def to_model_tool(self, chat_model: BaseChatModel) -> Dict[str, Any]: + """Convert this MCP tool definition to a model-specific tool dict. + + Default implementation is not provided; subclasses must override. + """ + raise NotImplementedError( + "to_model_tool must be implemented by MCPToolDefinition subclasses" + ) + + +class MCPPrompt(BaseModel): + """MCP prompt definition returned by list_prompts/get_prompt.""" + + name: str + description: Optional[str] = None + arguments: Dict[str, Any] = Field(default_factory=dict) + + +class MCPServer(BaseTool, ABC): + """Resource representing an MCP server and exposing its tools/prompts. + + This is a logical tool container; it is not directly invokable with call(). + """ + + endpoint: str + metadata: ToolMetadata = Field( # default metadata for the server wrapper + default_factory=lambda: ToolMetadata( + name="mcp_server", + description="A container for tools provided by an MCP server.", + args_schema=BaseModel, # not used + ) + ) + + @classmethod + @override + def resource_type(cls) -> ResourceType: # type: ignore[override] + return ResourceType.MCP_SERVER + + @classmethod + @override + def tool_type(cls) -> ToolType: + return ToolType.MCP + + def call_tool(self, name: str,*args: Any, **kwargs: Any) -> Any: # noqa: D401 Review Comment: Would it be nicer to call the tools directly on the `MCPTool` returned from `MCPServer.list_tools()` or `MCPServer.get_tool()`? In this way, the caller side won't need to understand whether the tool being called is form a mcp server or a local java/python function. ########## python/flink_agents/api/tools/mcp.py: ########## @@ -0,0 +1,132 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional, Tuple + +from pydantic import BaseModel, Field +from typing_extensions import override + +from flink_agents.api.resource import ResourceType +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.tools.tool import BaseTool, ToolMetadata, ToolType + + +class MCPToolDefinition(BaseModel, ABC): + """MCP tool definition returned by list_tools or get_tool_definition.""" + + name: str + description: str + parameters: Dict[str, Any] = Field(default_factory=dict) + + def to_model_tool(self, chat_model: BaseChatModel) -> Dict[str, Any]: + """Convert this MCP tool definition to a model-specific tool dict. + + Default implementation is not provided; subclasses must override. + """ + raise NotImplementedError( + "to_model_tool must be implemented by MCPToolDefinition subclasses" + ) + + +class MCPPrompt(BaseModel): + """MCP prompt definition returned by list_prompts/get_prompt.""" + + name: str + description: Optional[str] = None + arguments: Dict[str, Any] = Field(default_factory=dict) + + +class MCPServer(BaseTool, ABC): + """Resource representing an MCP server and exposing its tools/prompts. + + This is a logical tool container; it is not directly invokable with call(). + """ + + endpoint: str + metadata: ToolMetadata = Field( # default metadata for the server wrapper + default_factory=lambda: ToolMetadata( + name="mcp_server", + description="A container for tools provided by an MCP server.", + args_schema=BaseModel, # not used + ) + ) + + @classmethod + @override + def resource_type(cls) -> ResourceType: # type: ignore[override] + return ResourceType.MCP_SERVER + + @classmethod + @override + def tool_type(cls) -> ToolType: + return ToolType.MCP + + def call_tool(self, name: str,*args: Any, **kwargs: Any) -> Any: # noqa: D401 Review Comment: Like: ``` tool = mcp_server.get_tool(name) tool.call() ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
