-
Notifications
You must be signed in to change notification settings - Fork 8
support upload #20
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
support upload #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# v1.2.0 | ||
- 支持文件上传至七牛 Bucket | ||
|
||
# v1.1.1 | ||
- 支持 AK、SK 为空字符串 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ | |
from mcp import types | ||
from urllib.parse import unquote | ||
|
||
from mcp.server.lowlevel.helper_types import ReadResourceContents | ||
|
||
from .storage import StorageService | ||
from ...consts import consts | ||
from ...resource import resource | ||
from ...resource.resource import ResourceContents | ||
|
||
logger = logging.getLogger(consts.LOGGER_NAME) | ||
|
||
|
@@ -88,7 +91,7 @@ async def process_bucket_with_semaphore(bucket): | |
logger.info(f"Returning {len(resources)} resources") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use lazy % formatting in logging functions (logging-fstring-interpolation) Detailslint 解释
错误用法以下是一个错误的示例,展示了在日志记录函数中使用了传统的 import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
user_id = 123
username = "john_doe"
logger.debug("User %d logged in as %s" % (user_id, username)) 在这个示例中, 正确用法以下是一个正确的示例,展示了如何使用懒惰的字符串格式化: import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
user_id = 123
username = "john_doe"
logger.debug("User %d logged in as %s", user_id, username) 在这个示例中,
|
||
return resources | ||
|
||
async def read_resource(self, uri: types.AnyUrl, **kwargs) -> str: | ||
async def read_resource(self, uri: types.AnyUrl, **kwargs) -> ResourceContents: | ||
""" | ||
Read content from an S3 resource and return structured response | ||
|
||
|
@@ -120,7 +123,7 @@ async def read_resource(self, uri: types.AnyUrl, **kwargs) -> str: | |
if content_type.startswith("image/"): | ||
file_content = base64.b64encode(file_content).decode("utf-8") | ||
|
||
return file_content | ||
return [ReadResourceContents(mime_type=content_type, content=file_content)] | ||
|
||
|
||
def register_resource_provider(storage: StorageService): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,44 @@ async def get_object(self, bucket: str, key: str) -> Dict[str, Any]: | |
response["Body"] = b"".join(chunks) | ||
return response | ||
|
||
def upload_text_data(self, bucket: str, key: str, data: str, overwrite: bool = False) -> list[dict[str:Any]]: | ||
policy = { | ||
"insertOnly": 1, | ||
} | ||
|
||
if overwrite: | ||
policy["insertOnly"] = 0 | ||
policy["scope"] = f"{bucket}:{key}" | ||
|
||
token = self.auth.upload_token(bucket=bucket, key=key, policy=policy) | ||
ret, info = qiniu.put_data(up_token=token, key=key, data=bytes(data, encoding="utf-8")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused variable 'ret' (unused-variable) Detailslint 解释
错误用法以下是一个示例代码片段,展示了不正确的用法: def calculate_sum(a, b):
ret = a + b # 定义了一个未使用的变量 'ret'
return ret 在这个例子中,变量 正确用法以下是一个示例代码片段,展示了正确的用法: def calculate_sum(a, b):
return a + b # 直接返回计算结果,不需要额外的变量 'ret' 在这个例子中,直接返回了计算结果
|
||
if info.status_code != 200: | ||
raise Exception(f"Failed to upload object: {info}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Raising too general exception: Exception (broad-exception-raised) Detailslint 解释这个lint结果表明在代码中抛出了一个过于通用的异常 错误用法def divide(a, b):
try:
result = a / b
except Exception as e:
print(f"Error: {e}")
return result 在这个例子中, 正确用法def divide(a, b):
try:
result = a / b
except ZeroDivisionError as e:
print(f"Error: {e}")
return result 在这个例子中,
|
||
|
||
return self.get_object_url(bucket, key) | ||
|
||
def upload_local_file(self, bucket: str, key: str, file_path: str, overwrite: bool = False) -> list[dict[str:Any]]: | ||
policy = { | ||
"insertOnly": 1, | ||
} | ||
|
||
if overwrite: | ||
policy["insertOnly"] = 0 | ||
policy["scope"] = f"{bucket}:{key}" | ||
|
||
token = self.auth.upload_token(bucket=bucket, key=key, policy=policy) | ||
ret, info = qiniu.put_file(up_token=token, key=key, file_path=file_path) | ||
if info.status_code != 200: | ||
raise Exception(f"Failed to upload object: {info}") | ||
|
||
return self.get_object_url(bucket, key) | ||
|
||
def fetch_object(self, bucket: str, key: str, url: str): | ||
ret, info = self.bucket_manager.fetch(url, bucket, key=key) | ||
if info.status_code != 200: | ||
raise Exception(f"Failed to fetch object: {info}") | ||
|
||
return self.get_object_url(bucket, key) | ||
|
||
def is_text_file(self, key: str) -> bool: | ||
text_extensions = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
|
||
VERSION = '1.1.1' | ||
VERSION = '1.2.0' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing module docstring (missing-module-docstring) Detailslint 解释
错误用法以下是一个缺少模块文档字符串的示例: # resource.py
def get_resource():
return "Resource data" 在这个例子中, 正确用法以下是添加了模块文档字符串的正确示例: """
This module provides functions to manage resources.
"""
def get_resource():
"""
Returns the resource data.
:return: Resource data as a string.
"""
return "Resource data" 在这个例子中,
|
||
from abc import abstractmethod | ||
from typing import Dict, AsyncGenerator | ||
from typing import Dict, AsyncGenerator, Iterable | ||
|
||
from mcp import types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unable to import 'mcp' (import-error) Detailslint 解释这个lint结果表明在代码中尝试导入一个名为 错误用法# 错误的导入方式
import mcp 正确用法
通过以上步骤,你应该能够解决
|
||
from mcp.server.lowlevel.helper_types import ReadResourceContents | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unable to import 'mcp.server.lowlevel.helper_types' (import-error) Detailslint 解释这个lint结果表明在代码中尝试导入 错误用法from mcp.server.lowlevel import helper_types 在这个示例中,代码试图从 正确用法要解决这个问题,需要确保以下几点:
from src.mcp_server.resource.lowlevel import helper_types
例如: pip install mcp-server-lowlevel 通过以上步骤,应该能够解决导入错误的问题。
|
||
|
||
from ..consts import consts | ||
|
||
logger = logging.getLogger(consts.LOGGER_NAME) | ||
|
||
ResourceContents = str | bytes | Iterable[ReadResourceContents] | ||
|
||
class ResourceProvider: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing class docstring (missing-class-docstring) Detailslint 解释
错误用法class MyClass:
def my_method(self):
pass 在这个例子中, 正确用法class MyClass:
"""这是一个示例类"""
def my_method(self):
"""这是一个示例方法"""
pass 在这个例子中,
|
||
def __init__(self, scheme: str): | ||
|
@@ -17,7 +20,7 @@ async def list_resources(self, **kwargs) -> list[types.Resource]: | |
pass | ||
|
||
@abstractmethod | ||
async def read_resource(self, uri: types.AnyUrl, **kwargs) -> str: | ||
async def read_resource(self, uri: types.AnyUrl, **kwargs) -> ResourceContents: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing function or method docstring (missing-function-docstring) Detailslint 解释这个lint结果表明在代码中缺少函数或方法的文档字符串(docstring)。文档字符串是用于描述函数、类或模块用途和行为的字符串,通常放在定义之前。它有助于其他开发者理解代码的功能。 错误用法def calculate_sum(a, b):
return a + b 在这个例子中, 正确用法def calculate_sum(a, b):
"""
计算两个数的和
参数:
a (int): 第一个加数
b (int): 第二个加数
返回:
int: 两个数的和
"""
return a + b 在这个例子中,
|
||
pass | ||
|
||
|
||
|
@@ -35,7 +38,7 @@ async def list_resources(**kwargs) -> AsyncGenerator[types.Resource, None]: | |
return | ||
|
||
|
||
async def read_resource(uri: types.AnyUrl, **kwargs) -> str: | ||
async def read_resource(uri: types.AnyUrl, **kwargs) -> ResourceContents: | ||
if len(_all_resource_providers) == 0: | ||
return "" | ||
|
||
|
@@ -52,6 +55,7 @@ def register_resource_provider(provider: ResourceProvider): | |
|
||
|
||
__all__ = [ | ||
"ResourceContents", | ||
"ResourceProvider", | ||
"list_resources", | ||
"read_resource", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
standard import "urllib.parse.unquote" should be placed before third party import "mcp.types" (wrong-import-order)
Details
lint 解释
这个lint结果表明在代码文件
src/mcp_server/core/storage/resource.py
中,标准库的导入语句import urllib.parse.unquote
应该放在第三方库的导入语句import mcp.types
之前。按照PEP 8(Python编码风格指南)的规定,标准库的导入应该在第三方库的导入之前。错误用法
在这个错误示例中,
mcp.types
是一个第三方库的导入语句,而urllib.parse.unquote
是标准库的导入语句。正确的顺序应该是先导入标准库,再导入第三方库。正确用法
在这个正确示例中,
urllib.parse.unquote
是标准库的导入语句,而mcp.types
是第三方库的导入语句。按照PEP 8的规定,这种顺序是正确的。