Skip to content

Commit a24b793

Browse files
authored
Add files via upload
1 parent 0b97d71 commit a24b793

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5648
-0
lines changed

MCP/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
from ._mcp_function import MCPToolFunction
3+
from ._client_base import MCPClientBase
4+
from ._stateful_client_base import StatefulClientBase
5+
from ._http_stateless_client import HttpStatelessClient
6+
from ._stdio_stateful_client import StdIOStatefulClient
7+
8+
__all__ = [
9+
'MCPToolFunction',
10+
'MCPClientBase',
11+
'StatefulClientBase',
12+
'HttpStatelessClient',
13+
'StdIOStatefulClient'
14+
]
509 Bytes
Binary file not shown.
4.53 KB
Binary file not shown.
6.87 KB
Binary file not shown.
4.11 KB
Binary file not shown.
6.65 KB
Binary file not shown.
3.17 KB
Binary file not shown.
11.2 KB
Binary file not shown.
8.77 KB
Binary file not shown.

MCP/_client_base.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
"""The base class for MCP clients in AgentScope."""
3+
from abc import abstractmethod
4+
from dataclasses import dataclass
5+
from typing import Callable, List
6+
7+
import mcp.types
8+
9+
from _logging import logger
10+
11+
12+
@dataclass
13+
class ImageBlock:
14+
type: str
15+
source: "Base64Source"
16+
17+
18+
@dataclass
19+
class Base64Source:
20+
type: str
21+
media_type: str
22+
data: str
23+
24+
25+
@dataclass
26+
class AudioBlock:
27+
type: str
28+
source: Base64Source
29+
30+
31+
@dataclass
32+
class TextBlock:
33+
type: str
34+
text: str
35+
36+
37+
class MCPClientBase:
38+
"""Base class for MCP clients."""
39+
40+
def __init__(self, name: str) -> None:
41+
"""Initialize the MCP client with a name.
42+
43+
Args:
44+
name (`str`):
45+
The name to identify the MCP server, which should be unique
46+
across the MCP servers.
47+
"""
48+
self.name = name
49+
50+
@abstractmethod
51+
async def get_callable_function(
52+
self,
53+
func_name: str,
54+
wrap_tool_result: bool = True,
55+
) -> Callable:
56+
"""Get a tool function by its name."""
57+
58+
@staticmethod
59+
def _convert_mcp_content_to_as_blocks(
60+
mcp_content_blocks: list,
61+
) -> List[TextBlock | ImageBlock | AudioBlock]:
62+
"""Convert MCP content to AgentScope blocks."""
63+
64+
as_content: list = []
65+
for content in mcp_content_blocks:
66+
if isinstance(content, mcp.types.TextContent):
67+
as_content.append(
68+
TextBlock(
69+
type="text",
70+
text=content.text,
71+
),
72+
)
73+
elif isinstance(content, mcp.types.ImageContent):
74+
as_content.append(
75+
ImageBlock(
76+
type="image",
77+
source=Base64Source(
78+
type="base64",
79+
media_type=content.mimeType,
80+
data=content.data,
81+
),
82+
),
83+
)
84+
elif isinstance(content, mcp.types.AudioContent):
85+
as_content.append(
86+
AudioBlock(
87+
type="audio",
88+
source=Base64Source(
89+
type="base64",
90+
media_type=content.mimeType,
91+
data=content.data,
92+
),
93+
),
94+
)
95+
elif isinstance(content, mcp.types.EmbeddedResource):
96+
if isinstance(
97+
content.resource,
98+
mcp.types.TextResourceContents,
99+
):
100+
as_content.append(
101+
TextBlock(
102+
type="text",
103+
text=content.resource.model_dump_json(indent=2),
104+
),
105+
)
106+
else:
107+
# TODO: support the BlobResourceContents in the future,
108+
# which is a base64-encoded string representing the
109+
# binary data
110+
logger.error(
111+
"Unsupported EmbeddedResource content type: %s. "
112+
"Skipping this content.",
113+
type(content.resource),
114+
)
115+
else:
116+
logger.warning(
117+
"Unsupported content type: %s. Skipping this content.",
118+
type(content),
119+
)
120+
return as_content

0 commit comments

Comments
 (0)