From 82fb5cd711800c1b4fd0b6e7cf1a3de7801272cf Mon Sep 17 00:00:00 2001 From: wolvever Date: Fri, 28 Mar 2025 13:54:07 +0800 Subject: [PATCH] Optimize MCP servers file structure and update README --- python/mcp/README.md | 87 +++++++++++++++++++ .../{modelcontextprotocol => mcp}/__init__.py | 0 python/mcp/ai_search/README.md | 26 ++++++ python/mcp/ai_search/__init__.py | 5 ++ .../ai_search}/ai_search_server.py | 10 +-- .../{modelcontextprotocol => mcp}/client.py | 0 .../{modelcontextprotocol => mcp}/server.py | 0 python/modelcontextprotocol/README.md | 71 --------------- 8 files changed, 123 insertions(+), 76 deletions(-) create mode 100644 python/mcp/README.md rename python/{modelcontextprotocol => mcp}/__init__.py (100%) create mode 100644 python/mcp/ai_search/README.md create mode 100644 python/mcp/ai_search/__init__.py rename python/{modelcontextprotocol => mcp/ai_search}/ai_search_server.py (93%) rename python/{modelcontextprotocol => mcp}/client.py (100%) rename python/{modelcontextprotocol => mcp}/server.py (100%) delete mode 100644 python/modelcontextprotocol/README.md diff --git a/python/mcp/README.md b/python/mcp/README.md new file mode 100644 index 000000000..67f7da309 --- /dev/null +++ b/python/mcp/README.md @@ -0,0 +1,87 @@ +# MCP Component Server + +The MCP Component Server is a FastMCP server based implementation that converts AppBuilder Components into FastMCP tools, enabling seamless integration of Baidu cloud AI services into MCP-compatible environments. + +## Overview + +The `MCPComponentServer` class provides a bridge between AppBuilder Components and FastMCP tools, allowing you to: +- Convert AppBuilder Components into MCP-compatible tools +- Handle various content types (text, images, audio, references) +- Manage visibility scopes for different audiences +- Support streaming responses through generators + +## Features + +- Automatic conversion of AppBuilder Components to MCP tools +- Support for multiple content types: + - Text content + - Image content + - Audio content + - Reference content +- Configurable host and port settings +- Built-in error handling and logging +- Support for custom tool registration +- Automatic MIME type detection for media content + +## Usage + +### Basic Setup + +```python +from appbuilder import GeneralOCR, TextGeneration +from mcp.server import MCPComponentServer + +# Create server instance +server = MCPComponentServer("AI Service", host="localhost", port=8000) + +# Add AppBuilder components +ocr = GeneralOCR() +server.add_component(ocr) + +text_gen = TextGeneration() +server.add_component(text_gen) + +# Run the server +server.run() +``` + +### Adding Custom Tools + +```python +@server.tool() +def custom_function(param1: str, param2: int) -> str: + """Custom tool description""" + return f"Processed: {param1} {param2}" +``` + +### Adding Resources + +```python +@server.resource() +def get_resource(): + """Resource description""" + return {"data": "resource content"} +``` + +## Content Type Handling + +The server automatically handles various content types: + +1. **Text Content**: Converts text outputs to MCP TextContent +2. **Image Content**: Handles both base64 and URL-based images +3. **Audio Content**: Processes audio files with automatic MIME type detection +4. **Reference Content**: Manages document references and citations + + +## Configuration + +The server can be configured with various parameters: + +```python +server = MCPComponentServer( + name="Service Name", + host="localhost", # Default: "localhost" + port=8000, # Default: 8000 + **kwargs # Additional FastMCP arguments +) +``` diff --git a/python/modelcontextprotocol/__init__.py b/python/mcp/__init__.py similarity index 100% rename from python/modelcontextprotocol/__init__.py rename to python/mcp/__init__.py diff --git a/python/mcp/ai_search/README.md b/python/mcp/ai_search/README.md new file mode 100644 index 000000000..808a86a81 --- /dev/null +++ b/python/mcp/ai_search/README.md @@ -0,0 +1,26 @@ +# Baidu AI Search + +Baidu AI Search component combines Baidu's search capabilities with large language model technology to provide intelligent responses with real-time web information references, supporting various industry application scenarios. It offers rich standardized capabilities such as: + +- Custom persona settings +- Model selection +- Query rewriting (including time-sensitive and multi-turn approaches to enhance search results) +- Search scope configuration (choice of modalities, site ranges and publication dates) +- Customizable number of reference links + +## Quick Start + +1. Get your AppBuilder API Key from the console +2. Format your authorization token as: `Bearer+` (keep the "+" in between) +3. Config with + + { + "mcpServers": { + "baidu_ai_search": { + "url": "http://appbuilder.baidu.com/v2/ai_search/mcp/sse?api_key=Bearer+bce-v3/ALTAK..." + } + } + } + + +For more details, please refer to: https://cloud.baidu.com/doc/AppBuilder/s/zm8pn5cju diff --git a/python/mcp/ai_search/__init__.py b/python/mcp/ai_search/__init__.py new file mode 100644 index 000000000..5f6c6d033 --- /dev/null +++ b/python/mcp/ai_search/__init__.py @@ -0,0 +1,5 @@ + +from .ai_search_server import search + +__all__ = ["search"] + diff --git a/python/modelcontextprotocol/ai_search_server.py b/python/mcp/ai_search/ai_search_server.py similarity index 93% rename from python/modelcontextprotocol/ai_search_server.py rename to python/mcp/ai_search/ai_search_server.py index ae183896d..502e9e953 100644 --- a/python/modelcontextprotocol/ai_search_server.py +++ b/python/mcp/ai_search/ai_search_server.py @@ -1,10 +1,10 @@ """ -Baidu AI Search MCP Server stdio server file. +Baidu AI Search MCP Server. We also support access via SSE protocol. The access address is: http://appbuilder.baidu.com/v2/ai_search/mcp/sse?api_key= You can refer to this webpage https://cloud.baidu.com/doc/AppBuilder/s/klv2eywua to obtain the api_key, in the format of "Bearer+bce…". """ -import os + import json from mcp.server import FastMCP from appbuilder.core.components.rag_with_baidu_search_pro import RagWithBaiduSearchPro @@ -22,7 +22,7 @@ @server.tool() -def AIsearch( +def search( query, stream=False, instruction=None, @@ -50,10 +50,10 @@ def AIsearch( AppBuilderServerException: 如果输入信息或指令过长,将抛出此异常。 """ message = SimpleNamespace(role="user", content="{}".format(query)) - search_instance = RagWithBaiduSearchPro( + component = RagWithBaiduSearchPro( model=init_args["model"] ) - response = search_instance.run( + response = component.run( message=message, stream=stream, instruction=instruction, diff --git a/python/modelcontextprotocol/client.py b/python/mcp/client.py similarity index 100% rename from python/modelcontextprotocol/client.py rename to python/mcp/client.py diff --git a/python/modelcontextprotocol/server.py b/python/mcp/server.py similarity index 100% rename from python/modelcontextprotocol/server.py rename to python/mcp/server.py diff --git a/python/modelcontextprotocol/README.md b/python/modelcontextprotocol/README.md deleted file mode 100644 index 34341ae04..000000000 --- a/python/modelcontextprotocol/README.md +++ /dev/null @@ -1,71 +0,0 @@ -### Baidu AISearch MCP Server - -An MCP server implementation that integrates the Baidu AI search API, providing web search capabilities and summary of LLM. The Baidu AI Search Component combines Baidu Search capabilities with large model technology, providing intelligent response functions with real-time information from the entire network, and supporting a variety of industry scenarios. - -### Features - -* **Integration of Search and Large Model Technology**: The component seamlessly merges Baidu’s powerful search engine with advanced large model technology, enabling intelligent and contextually aware responses. -* **Real-time Information Access**: It provides users with up-to-date information from across the internet, ensuring relevant and timely replies. -* **Versatile Applications**: The component caters to a wide range of industries and scenarios, offering flexible solutions for various use cases. -* **Standardized and Customizable Features**: Users can customize personas, choose from different models, rewrite questions for enhanced search results, configure search scopes, and specify the number of reference links, among other options. -* **Enhanced Performance and Availability**: The API delivers high performance and reliability, ensuring smooth and uninterrupted service. -* **Comprehensive Content Safety**: With rigorous content safety reviews, the component ensures all responses and information remain compliant and within acceptable standards. - -### Tools - -* AIsearch - * Execute web searches with pagination and filtering - * Inputs: - - query (str): The search request. - - stream (bool, optional): Whether to receive response data in streaming format. Defaults to False. - - instruction (Instruction, optional): Instruction information object. Defaults to None. - - temperature (float, optional): Temperature parameter to control the randomness of generated text. Defaults to 1e-10. - - top_p (float, optional): Cumulative probability threshold for controlling the diversity of generated text. Defaults to 1e-10. - - search_top_k (int, optional): The number of search candidate results. Defaults to 4. - - hide_corner_markers (bool, optional): Whether to hide the boundary markers in the response. Defaults to True. - -### Configuration - -#### Getting an API Key - -You can refer to this webpage https://cloud.baidu.com/doc/AppBuilder/s/klv2eywua to obtain the api_key - -#### Usage with Claude Desktop - -Add this to your `claude_desktop_config.json`: - -##### python -```json -{ - "mcpServers": { - "AB Component Server": { - "command": "/path/to/your/python3.12", - "args": [ - "/path/to/your/ai_search_server.py" - ], - "envs": { - "APPBUILDER_TOKEN": "{AppBuilder API Key}" - } - } - } -} -``` - -#### Usage with Cursor - -* the format of api_key is “Bearer+”, note that “+” in the middle should be retained, example: Bearer+bce-v3/ALTAK-xuZRMCVTC9###### - -```json - -{ - "mcpServers": { - "AISearch": { - "url": "http://appbuilder.baidu.com/v2/ai_search/mcp/sse?api_key={Bearer+}" - } - } -} -``` - -### License - -Copyright (c) 2024 Baidu, Inc. All Rights Reserved. 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. \ No newline at end of file