本项目基于 ddddocr 和 Model Context Protocol (MCP) Python SDK,实现了一个通过 MCP 协议对外提供验证码图片识别服务的智能后端。
- ddddocr
- mcp[fastmcp]
- 克隆本项目
git clone <your-repo-url> cd <your-project-dir>
- 安装依赖
pip install ddddocr mcp[fastmcp]
python main.pyfrom mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import asyncio
async def main():
server_params = StdioServerParameters(
command="python",
args=["main.py"],
env=None,
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(
"recognize_captcha",
arguments={"image_path": "E:/code/bug/service/ocr/captchaImage.jpg"}
)
print("识别结果:", result)
if __name__ == "__main__":
asyncio.run(main())recognize_captcha(image_path: str)
识别本地图片验证码,返回内容对象
{
"mcpServers": {
"ocr": {
"command": "F:\\AI\\anaconda3\\Scripts\\uv.exe",
"args": [
"run",
"--with",
"mcp",
"mcp",
"run",
"E:\\code\\bug\\service\\ocr\\mcp-server-demo\\main.py"
]
}
}
}MIT