Skip to content

Commit 9287187

Browse files
feat: optimize miku MCP server - remove unused import and add stream creation API
1. Remove unused import mcp.types.TextContent from tools.py 2. Add create_stream API implementation with S3-style format: https://<bucket>.<endpoint>/<stream> 3. Add miku_create_stream MCP tool for stream creation Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: callmefisher <[email protected]>
1 parent 37dd6c0 commit 9287187

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/mcp_server/core/miku/miku.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ def _build_bucket_url(self, bucket: str) -> str:
3737
# Build URL in format: https://<bucket>.<endpoint>
3838
return f"https://{bucket}.{endpoint}"
3939

40+
def _build_stream_url(self, bucket: str, stream: str) -> str:
41+
"""Build S3-style stream URL"""
42+
if not self.endpoint_url:
43+
raise ValueError("QINIU_ENDPOINT_URL is not configured")
44+
45+
# Remove protocol if present in endpoint_url
46+
endpoint = self.endpoint_url
47+
if endpoint.startswith("http://"):
48+
endpoint = endpoint[7:]
49+
elif endpoint.startswith("https://"):
50+
endpoint = endpoint[8:]
51+
52+
# Build URL in format: https://<bucket>.<endpoint>/<stream>
53+
return f"https://{bucket}.{endpoint}/{stream}"
54+
4055
async def create_bucket(self, bucket: str) -> Dict[str, Any]:
4156
"""
4257
Create a bucket using S3-style API
@@ -75,3 +90,45 @@ async def create_bucket(self, bucket: str) -> Dict[str, Any]:
7590
"message": f"Failed to create bucket: {text}",
7691
"status_code": status
7792
}
93+
94+
async def create_stream(self, bucket: str, stream: str) -> Dict[str, Any]:
95+
"""
96+
Create a stream using S3-style API
97+
98+
Args:
99+
bucket: The bucket name
100+
stream: The stream name to create
101+
102+
Returns:
103+
Dict containing the response status and message
104+
"""
105+
url = self._build_stream_url(bucket, stream)
106+
headers = self._get_auth_header()
107+
108+
logger.info(f"Creating stream: {stream} in bucket: {bucket} at {url}")
109+
110+
async with aiohttp.ClientSession() as session:
111+
async with session.put(url, headers=headers) as response:
112+
status = response.status
113+
text = await response.text()
114+
115+
if status == 200 or status == 201:
116+
logger.info(f"Successfully created stream: {stream} in bucket: {bucket}")
117+
return {
118+
"status": "success",
119+
"bucket": bucket,
120+
"stream": stream,
121+
"url": url,
122+
"message": f"Stream '{stream}' created successfully in bucket '{bucket}'",
123+
"status_code": status
124+
}
125+
else:
126+
logger.error(f"Failed to create stream: {stream}, status: {status}, response: {text}")
127+
return {
128+
"status": "error",
129+
"bucket": bucket,
130+
"stream": stream,
131+
"url": url,
132+
"message": f"Failed to create stream: {text}",
133+
"status_code": status
134+
}

src/mcp_server/core/miku/tools.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22

33
from mcp import types
4-
from mcp.types import TextContent
54

65
from .miku import MikuService
76
from ...consts import consts
@@ -10,6 +9,7 @@
109
logger = logging.getLogger(consts.LOGGER_NAME)
1110

1211
_BUCKET_DESC = "Miku bucket name"
12+
_STREAM_DESC = "Miku stream name"
1313

1414

1515
class _ToolImpl:
@@ -36,11 +36,36 @@ async def create_bucket(self, **kwargs) -> list[types.TextContent]:
3636
result = await self.miku.create_bucket(**kwargs)
3737
return [types.TextContent(type="text", text=str(result))]
3838

39+
@tools.tool_meta(
40+
types.Tool(
41+
name="miku_create_stream",
42+
description="Create a new stream in Miku using S3-style API. The stream will be created at https://<bucket>.<endpoint_url>/<stream>",
43+
inputSchema={
44+
"type": "object",
45+
"properties": {
46+
"bucket": {
47+
"type": "string",
48+
"description": _BUCKET_DESC,
49+
},
50+
"stream": {
51+
"type": "string",
52+
"description": _STREAM_DESC,
53+
},
54+
},
55+
"required": ["bucket", "stream"],
56+
},
57+
)
58+
)
59+
async def create_stream(self, **kwargs) -> list[types.TextContent]:
60+
result = await self.miku.create_stream(**kwargs)
61+
return [types.TextContent(type="text", text=str(result))]
62+
3963

4064
def register_tools(miku: MikuService):
4165
tool_impl = _ToolImpl(miku)
4266
tools.auto_register_tools(
4367
[
4468
tool_impl.create_bucket,
69+
tool_impl.create_stream,
4570
]
4671
)

0 commit comments

Comments
 (0)