Server Introduction
In a typical RAG system, the overall process usually consists of multiple functional modules, such as Retriever, Generator, etc. These modules undertake different tasks and work synergistically through process orchestration to complete complex question-answering and reasoning processes. In UltraRAG, based on the MCP (Model Context Protocol) architecture, we have uniformly encapsulated these functional modules and proposed a more standardized implementation method — Server.A Server is essentially a RAG module component with independent functions.
Server Development
To help you better understand how to use Server, this section will demonstrate the complete development process of building a custom Server from scratch through a simple example.Step 1: Create Server File
First, create a folder namedsayhello under the servers folder, and create a source code directory sayhello/src in it. Then, create a file sayhello.py in the src directory as the main program entry of the Server.
In UltraRAG, all Servers are instantiated through the base class UltraRAG_MCP_Server. The example is as follows:
servers/sayhello/src/sayhello.py
Step 2: Implement Tool Functions
Use the@app.tool decorator to register tool functions (Tool). These functions will be called during the Pipeline execution process to implement specific functional logic.
For example, the following example defines the simplest greeting function greet, which inputs a name and returns the corresponding greeting:
servers/sayhello/src/sayhello.py
Step 3: Configure Parameter File
Next, create a parameter configuration fileparameter.yaml under the sayhello folder. This file is used to declare the input parameters required by the Tool and their default values, facilitating automatic loading and passing during Pipeline runtime.
The example is as follows:
name is defined with a default value of “UltraRAG v3”.
Parameter Registration Mechanism
If there are parameter naming conflicts between different Prompt Tools, please refer to the “Multi-Prompt Tool Calling Scenario” section in Prompt Server for solutions.
parameter.yaml file in each Server directory during the build phase, and perceives and registers the parameters required by tool functions accordingly. When using, please note the following points:
- Parameter Sharing Mechanism: When multiple Tools need to share the same parameter (such as template, model_name_or_path, etc.), it can be declared only once in
parameter.yamland reused without repeated definition. - Field Overwrite Risk: If the parameters required by multiple Tools have the same name but different meanings or default values, the field names should be explicitly distinguished using different names to avoid being overwritten in the automatically generated configuration file.
- Context Automatic Inference Mechanism: If some input parameters in the tool function do not appear in
parameter.yaml, UltraRAG will default to attempting to infer from the runtime context (i.e., obtaining from the output of upstream Tools). Therefore, it is only necessary to explicitly define inparameter.yamlwhen parameters cannot be automatically passed through context.
Encapsulating Shared Variables via Class
In some scenarios, we may want to maintain shared state or variables within the same Server, such as model instances, cache objects, configurations, etc. In this case, the Server can be encapsulated as a class, and the definition of shared variables and Tool registration can be completed during the initialization phase of the class. The following example demonstrates how to encapsulate the sayhello Server as a class to achieve internal variable sharing:servers/sayhello/src/sayhello.py
self.sen is used to simulate variables that need to be shared between different Tools. This method is particularly suitable for scenarios that require loading models and repeated configuration parameters.