# coding: utf-8
#
# Copyright 2026 祁筱欣
# SPDX-License-Identifier: Apache-2.0
#
# Licensed 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.
#
"""
MCP 服务器核心模块
此模块定义了 MCP 服务器的核心功能,包括工具注册和服务器实例创建。
可以被不同的入口文件导入使用,支持 SSE 和 stdio 两种传输模式。
"""
from mcp.server.fastmcp import FastMCP
from mzmcp.providers.huaweicloud.ocr import recognize_web_image as _recognize_web_image
from mzmcp.providers.huaweicloud.moderation import check_image_moderation as _check_image_moderation
from mzmcp.providers.huaweicloud.celebrity import recognize_celebrity as _recognize_celebrity
# 创建 FastMCP 实例
mcp = FastMCP("MZ-MCP")
# 注册工具 - 使用装饰器正确注册
@mcp.tool()
async def recognize_web_image(
image_url: str = None, image: str = None, detect_direction: bool = None
) -> dict:
"""使用华为云 OCR 服务识别图片中的文字内容。
Args:
image_url (str, optional): 图片的 URL 地址(需可公开访问)。
image (str, optional): 图片的 Base64 编码。
detect_direction (bool, optional): 是否检测文字方向。
Returns:
dict: OCR 识别结果。
Note:
image_url 和 image 参数二选一。
"""
return await _recognize_web_image(
image_url=image_url, image=image, detect_direction=detect_direction
)
@mcp.tool()
async def check_image_moderation(
url: str = None, image_base64: str = None, image_file_path: str = None
) -> dict:
"""使用华为云内容审核服务检查图片是否包含违规内容。
Args:
url (str, optional): 图片的 URL 地址(需可公开访问)。
image_base64 (str, optional): 图片的 Base64 编码。
image_file_path (str, optional): 本地图片文件路径。
Returns:
dict: 内容审核结果,包含审核结果和相关信息。
Note:
url、image_base64 和 image_file_path 参数三选一。
使用默认的审核配置,无需额外参数。
"""
return await _check_image_moderation(
url=url, image_base64=image_base64, image_file_path=image_file_path
)
@mcp.tool()
async def recognize_celebrity(url: str = None, image: str = None) -> dict:
"""使用华为云名人识别服务识别图片中的名人。
Args:
url (str, optional): 图片的 URL 地址(需可公开访问)。
image (str, optional): 图片的 Base64 编码。
Returns:
dict: 名人识别结果。
Note:
url 和 image 参数二选一。
"""
return await _recognize_celebrity(url=url, image=image)