Dynamic screening large language model-based MCP tool calling method and system
By dynamically filtering the MCP tool invocation method of the large language model, and utilizing the Embedding model and hybrid query vector technology, the problems of resource waste and information overload under the massive tool library of the large language model are solved, and efficient and accurate tool invocation and system decision-making are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SHENZHEN HAIYUNAN NETWORK SECURITY TECH CO LTD
- Filing Date
- 2026-02-05
- Publication Date
- 2026-05-12
AI Technical Summary
When faced with a massive tool library, existing large language models suffer from wasted token resources and increased computational latency due to full context injection. Furthermore, in information-overloaded environments, they struggle to efficiently and accurately identify user intent, reducing the success rate and reliability of tool calls.
By parsing the MCP server-side tool set, static feature vectors are generated using a pre-trained embedding model. Combined with user input and historical dialogue records, a hybrid query vector is constructed. The semantic matching score, historical call success rate, and complexity penalty of the tools are calculated. A candidate tool set is dynamically selected, and a concise system prompt word is constructed for invocation.
It effectively frees up context window space, reduces inference latency and computing costs, improves the accuracy of tool calls and the reliability of system decisions, ensures that critical tools are retained when resources are scarce, and avoids interference from irrelevant information.
Smart Images

Figure CN122019211A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language data processing technology, specifically to a method and system for calling MCP tools based on dynamically filtering large language models. Background Technology
[0002] With the rapid development of artificial intelligence technology, Large Language Models (LLMs) have evolved from simple text generation tools into intelligent agents with autonomous planning and execution capabilities. In the intelligent agent application ecosystem, to endow models with the ability to perceive the environment and manipulate the physical world, it is usually necessary to connect to various backend tools or application programming interfaces (APIs) through Model Call Protocols (MCPs). As a standardized interaction protocol, MCPs greatly simplify the integration process between LLMs and external tools, enabling intelligent agents to handle complex tasks such as weather queries, database retrieval, and financial analysis.
[0003] In existing technical architectures, to enable large language models to recognize and invoke these external tools, systems typically employ a full context injection strategy. That is, in each round of dialogue, the function descriptions, parameter definitions (schemas), and invocation examples of all tools registered on the MCP server are input into the LLM's context window all at once, via system prompts. The model then generates the corresponding function call request based on the user's natural language commands and the tool definitions in the context, through inference.
[0004] However, with the increasing complexity of intelligent agent application scenarios, the number of tools integrated into the MCP server has exploded, expanding from a few core tools initially to hundreds or even thousands. This full context injection approach has revealed some shortcomings when faced with a massive tool library: on the one hand, the context window length of a large language model is limited, and the consumption of token resources directly corresponds to high inference costs and computational latency; the current approach indiscriminately fills the context with lengthy, detailed, and massive amounts of tool description text, causing tool definitions to occupy most of the available token space; this not only wastes computing resources but also greatly compresses the space used to store user history dialogues, core task instructions, and thought chain reasoning, limiting the model's ability to process long texts and complex logical tasks. On the other hand, presenting a vast, disordered set of tools containing a large amount of interfering information to an LLM without filtering is equivalent to requiring the model to search for useful information in an extremely noisy environment. Based on the attention mechanism characteristics of large models, when there is too much irrelevant context, the model's attention to key instructions will be diluted, making it difficult to efficiently and accurately identify the tool most suitable for the current user's intent from a massive pool of tools, thus reducing the success rate and reliability of tool invocation. Therefore, there is an urgent need for an MCP tool invocation method and system based on dynamic filtering of large language models to solve the above problems. Summary of the Invention
[0005] To address the problems in related technologies, this invention provides a method for calling MCP tools based on dynamically filtering large language models, thereby overcoming the aforementioned technical problems in existing related technologies.
[0006] To solve the aforementioned technical problem, the present invention is achieved through the following technical solution: In a first aspect, embodiments of the present invention provide a method for calling MCP tools based on a dynamic filtering large language model, specifically including: parsing all tool sets exposed by the MCP server, extracting the functional description and parameter schema information of each tool, and using a pre-trained embedding model to convert the functional description of each tool into a static feature vector; acquiring the user's current text input and historical dialogue records in the historical dialogue window in real time; constructing a hybrid query vector that integrates context information based on the user's current text input and historical dialogue records, and introducing a time decay factor; calculating the semantic matching score between the hybrid query vector and the static feature vector of each tool, obtaining the historical call success rate of each tool, calculating the schema complexity penalty term of each tool, and calculating the comprehensive score of each tool by weighted summation; calculating the statistical features of the comprehensive scores of all current tools, and calculating a dynamic filtering threshold based on the statistical features and the remaining space of the current context window; comparing the comprehensive score of each tool with the dynamic filtering threshold to filter out a set of candidate tools; constructing system prompt words containing a simplified tool list based on the selected set of candidate tools and sending them to the large language model for inference calling; if the set of candidate tools is empty, constructing system prompt words without a tool list for pure dialogue response.
[0007] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, the specific formula for the hybrid query vector is as follows: ; In the formula, Represents a mixed query vector. Represents a vector embedding function. This indicates the user's current text input. Indicates the first The content of the historical dialogue of the wheel, This represents the weight coefficient of the current input. The time decay factor representing historical information.
[0008] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, the formula for calculating the comprehensive score of each tool is as follows: ; In the formula, For tools Overall score For tools The semantic matching score, For tools Historical call success rate, For tools Complexity penalty term, , , These are the semantic item weights, historical performance item weights, and complexity penalty item weights, respectively, and they satisfy the following conditions: .
[0009] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, wherein the tool semantic matching score By calculating the mixed query vector With tool static feature vector The cosine similarity is obtained.
[0010] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, wherein the tool Historical call success rate The result is calculated based on the number of successful calls within a preset time window and the total number of calls, and then smoothed using Laplace's algorithm.
[0011] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, wherein the tool Complexity penalty term Token length based on tool schema Maximum value in the current toolset and minimum value Obtained by performing max-min normalization calculation.
[0012] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, the formula for calculating the dynamic filtering threshold is as follows: ; In the formula, As the filtering threshold, The arithmetic mean of the scores for all current tools. The standard deviation of the scores for all tools This is the sensitivity adjustment coefficient; If tools Overall rating If it is selected, it will be included in the candidate tool set; if If so, then remove it.
[0013] As a preferred embodiment of the MCP tool invocation method based on dynamic screening of large language models described in this invention, the formula for calculating the sensitivity adjustment coefficient is as follows: ; In the formula, The number of remaining available tokens; This is a truncation function, and the truncation range is... , It is a preset minimum positive number; The maximum context window limit for the currently invoked LLM model. This represents the total number of tokens currently in use during the session. This represents the total number of candidate tools currently participating in the screening process. The average token length of all tools currently participating in the screening. It is the inverse cumulative distribution function of the standard normal distribution.
[0014] As a preferred embodiment of the MCP tool invocation method based on dynamic filtering of large language models described in this invention, the construction of the system prompt word includes: if the candidate tool set is not empty, it is converted into a JSON description object conforming to the MCP protocol, and assembled into a system prompt word together with the role definition, user command, and context history dialogue record; if the candidate tool set is empty, the system prompt word does not contain any tool description information.
[0015] Secondly, embodiments of the present invention provide an MCP tool invocation system based on a dynamically filtered large language model, comprising: a feature extraction module, used to parse the MCP tool set, extract the functional descriptions of the tools and convert them into static feature vectors stored in a vector database, and to obtain the user's current text input and historical dialogue records in real time; a context fusion module, used to calculate a context-aware hybrid query vector based on the user's current input, historical dialogue records and time decay factor; a scoring module, used to calculate the semantic matching score, historical call success rate and complexity penalty term of each tool relative to the hybrid query vector, and to comprehensively calculate the multi-dimensional comprehensive relevance score of each tool; a dynamic filtering module, used to calculate a dynamic filtering threshold based on the statistical features of the comprehensive scores of all tools and the remaining space of the current context window, and to filter out a set of candidate tools accordingly; and an execution invocation module, used to adaptively construct system prompt words that include or do not include a simplified tool list according to the filtering results, and to invoke the large language model for reasoning or dialogue response.
[0016] The present invention has the following beneficial effects: 1. This invention introduces a complexity penalty item for the tool schema into the scoring mechanism and combines it with a dynamic filtering mechanism. This can automatically eliminate redundant tools that consume a lot of tokens and are unnecessary. As a result, the final generated prompts contain only a small number of high-value, concise tool lists, which greatly frees up context window space. This not only reduces the reasoning delay and computing cost caused by long texts, but also reserves sufficient character space for users' multi-turn dialogue history and complex thought chain reasoning.
[0017] 2. This invention abandons simple semantic matching and constructs a multi-dimensional comprehensive evaluation system that includes semantic similarity, historical call success rate, and complexity. By introducing the historical success rate index, it can automatically downgrade tools that are semantically related but often fail in actual execution, and use historical experience to correct current decisions. At the same time, the dynamically selected streamlined toolset reduces the dilution of irrelevant information on the model's attention mechanism, reduces the illusion phenomenon caused by information overload, and helps to improve the accuracy of tool calls and the reliability of agent decision-making in complex environments.
[0018] 3. This invention adopts a context-aware hybrid query vector construction method. By introducing a time decay factor to weight and fuse historical dialogue information, it captures and retains key entities and intent features in historical dialogues, so that the current query vector implicitly contains the context. This ensures that even when the user's instructions are incomplete, the system can still accurately recall tools that meet the actual needs, which helps to improve the fluency and intelligence of human-computer interaction.
[0019] 4. This invention calculates a dynamic truncation threshold based on the statistical distribution of current tool ratings, enabling the system to adaptively adjust the sensitivity of the filtering based on the remaining space and congestion level of the current context window. This ensures that more diverse tool choices are provided when token resources are abundant, while only the most critical tools are retained when resources are scarce. This maintains a balance between tool richness and context load, helping to avoid the problem of losing critical tools due to forced truncation.
[0020] Of course, any product implementing this invention does not necessarily need to achieve all of the advantages described above at the same time. Attached Figure Description
[0021] To more clearly illustrate the technical solutions of the embodiments of the invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the invention. For those skilled in the art, the drawings can be obtained from these drawings without creative effort.
[0022] Figure 1This invention provides a flowchart of an MCP tool invocation method based on dynamic filtering of large language models.
[0023] Figure 2 This invention provides a schematic diagram of a module for calling an MCP tool based on a dynamically filtered large language model. Detailed Implementation
[0024] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention. Example 1
[0025] Current Large Language Model (LLM) agent applications typically rely on Model Call Protocol (MCP) to invoke backend tools. However, with the surge in the number of tools integrated into the MCP server, directly feeding hundreds or thousands of tool descriptions into the LLM's context window at once leads to extreme waste of token resources, limiting the model's ability to process core task information. Furthermore, presenting the LLM with a vast and unordered set of tools without filtering is tantamount to requiring the model to make decisions in an environment of information overload and lack of focus. This makes it difficult for the LLM to efficiently and accurately identify and select the tool most suitable for the current user intent and context, thereby reducing the success rate and reliability of tool invocation.
[0026] To solve the above technical problems, such as Figure 1 As shown, Embodiment 1 of the present invention provides a method for calling MCP tools based on dynamic filtering of large language models. Specifically, Embodiment 1 takes a certain intelligent assistant application scenario as an example: the system has accessed a total of 100 MCP tools, including weather query, stock analysis, and complex scientific research database retrieval. The user is conducting multiple rounds of dialogue and needs the intelligent assistant to accurately call the tools according to the instructions.
[0027] In the specific implementation process of Example 1: First, all tool sets exposed by the MCP server are parsed, and the functional description and parameter schema information of each tool are extracted. The functional description of each tool is transformed into a static feature vector using a pre-trained Embedding model. The user's current text input and historical dialogue records in the historical dialogue window are acquired in real time. This method uses a pre-trained Embedding model to semantically encode the functional description and parameter schema of the tool to generate static feature vectors. At the same time, it captures the user's current text input and historical dialogue records, providing raw data for subsequent context-aware retrieval. It maps unstructured tool text into structured vectors, providing a basis for subsequent semantic similarity calculation, and ensuring that the system can perceive the temporal continuity of the dialogue, thereby improving the recall rate and semantic understanding depth of tool retrieval. Secondly, based on the user's current text input and historical dialogue records, and by introducing a time decay factor, a hybrid query vector incorporating contextual information is constructed. This method weights and fuses the embedding vector of the current query with the embedding vector of historical dialogues, and introduces a time decay factor to give recent dialogues higher weight, forming a query representation rich in contextual information. This achieves implicit completion of the dialogue context, enhances the ability of the query vector to represent the actual user intent, and improves the tool recall accuracy in scenarios with ambiguous expressions or unclear referents. Next, the semantic matching score between the hybrid query vector and the static feature vector of each tool is calculated, the historical call success rate of each tool is obtained, and the schema complexity penalty term for each tool is calculated. The comprehensive score of each tool is calculated by weighted summation. This method calculates the semantic similarity between the tool and the query vector, statistically analyzes the historical call success rate, and quantifies the complexity penalty term of the tool schema. Through weighted integration, a comprehensive score reflecting tool relevance, stability, and resource consumption is obtained. By introducing actual call experience and cost control mechanisms, tools with high success rates and simple structures are prioritized for recommendation while ensuring semantic relevance, thereby improving the overall robustness and inference efficiency of the system. Then, the statistical characteristics of the overall scores of all tools are calculated. Based on the statistical characteristics and the remaining space of the current context window, a dynamic filtering threshold is calculated. The overall score of each tool is compared with the dynamic filtering threshold to filter out a set of candidate tools. This method calculates the mean and standard deviation of the scores of all tools, and, combined with the remaining capacity of the context window, derives the dynamic filtering threshold through statistical methods. Only tools with scores higher than the filtering threshold are retained as candidates, thereby avoiding overly lenient or overly strict filtering caused by fixed-quantity truncation. It can flexibly adjust the filtering intensity according to the stage of the dialogue and resource status, ensuring that key tools are not missed while minimizing the injection of redundant information.Finally, based on the selected candidate tool set, a system prompt word containing a concise tool list is constructed and sent to the large language model for inference invocation; if the candidate tool set is empty, a system prompt word without a tool list is constructed for pure dialogue response, thereby achieving adaptive generation of prompt word content, injecting tool information only when necessary, thus saving context window space, reducing model attention distraction, and improving the accuracy and response speed of tool invocation.
[0028] Furthermore, to better illustrate the technical solution of Embodiment 1 of the present invention, a detailed description is provided of the method for calling the MCP tool based on dynamically filtering large language models, specifically including the following: S1. Construct the MCP tool feature vector library and context-aware vectors. The goal is to solve the problem of unclear intent in single-sentence commands by describing them through preprocessing tools and fusing them with historical dialogue information at runtime. This includes the following sub-steps: S11. First, the system exposes all the toolsets on the MCP server. Perform analysis. This represents the total number of tools. Specifically, the system extracts each tool... Functional description Includes parameter schema information and calculates the number of tokens used by each tool schema. .
[0029] S12. Using a pre-trained embedding model, describe the functionality of each tool. Transform into static feature vectors And store it in a vector database.
[0030] S13. Real-time acquisition of the user's current text input. and in the history dialog window Round of historical dialogue records .
[0031] In this embodiment 1, the system maps the discrete tool definition text registered on the MCP server into a continuous vector space representation through a preprocessing process, and simultaneously captures the current session context as the input stream. Specifically, for example, for the "weather query tool" in the toolset, the system uses an embedding model to transform its functional description "provides weather forecasts and clothing suggestions for major cities around the world for the next 7 days" into a static feature vector. And store it; when the user currently types "Do I need to bring an umbrella tomorrow?", the system not only retrieves that text... It also extracts the previous dialogue records. (e.g., "Help me check flights to Hangzhou"), and temporarily store this raw text data. This step, by converting unstructured natural language text into structured mathematical features, establishes a unified semantic metric space, solving the shortcomings of traditional hard-coding or keyword matching methods in understanding deep semantics. It provides a computable data foundation for subsequent tool location by combining historical context information (e.g., recognizing that "that" refers to "Hangzhou"), thereby improving the initial recall rate of the tool's retrieval.
[0032] S2. Calculate the context-aware hybrid query vector, which includes the following sub-steps: S21. To address the issue of ambiguous referencing in multi-turn dialogues (e.g., a user says "use that tool to process it"), the system bases its references on the user's current text input. and historical dialogue records And introduce a time decay factor Construct hybrid query vectors The specific formula is expressed as follows: ; In the formula, This represents the final query vector that incorporates contextual information; Represents a vector embedding function; This indicates the user's current text input; Indicates the first The content of the historical dialogue of the wheel ( (The most recent round) This represents the weighting coefficient of the current input, used to emphasize the current intent; This represents the time decay factor of historical information, ensuring that the further back in time the history is, the less impact it has on the current intention.
[0033] For example, in this embodiment 1, the currently input weight coefficient It can be trained by minimizing the intent prediction error on a large-scale historical call log, as follows: Constructing the definition of the semantic completeness scoring function For the current input vector Nonlinear mapping function: ; For dimension The weight vector ( (Embedding dimension) For bias terms; Constrain the output to Within the range.
[0034] Among them, parameters and The following was obtained through offline training based on the Embedding model and the Sigmoid activation function: Collect historical dialogue logs and construct no fewer than 1000 sample pairs. ,in The correct tool actually invoked in this round of dialogue; To maximize the use of the right tools The predicted probability is the optimization objective, and the loss function is defined as follows: ; With fixed Embedding model parameters, only for and Perform gradient updates until the loss function converges, i.e., the loss value is reached. It no longer decreases significantly with the number of iterations or tends to stabilize.
[0035] For example, in this embodiment 1, the time decay factor of historical information It can be calculated in real time based on the cosine similarity distribution of semantic vectors within the runtime history dialog window, as follows: First, calculate the semantic coherence value between adjacent rounds. For the history window... Each round of dialogue ( ), calculate its relationship with the next round of dialogue Cosine similarity: ; Secondly, the topic cohesion metric is calculated. Before calculating the semantic coherence value between adjacent rounds, the system will review historical dialogue records. Preprocessing is performed: Non-essential responses that hit a pre-defined stop-word list (containing phrases like "okay," "received," and "understood") are removed. Then, the average similarity across all adjacent rounds within the history window is defined as the current topic cohesion. ; The higher the value, the more likely the historical conversations have revolved around the same topic (such as continuously asking about the weather). In this case, more historical information should be retained. (Increases); conversely, it indicates frequent topic switching, and historical information should be quickly forgotten. (Become smaller).
[0036] Finally, the mapping generates dynamic factors. To ensure To accurately reflect the validity of historical information and avoid interference from irrelevant noise, this embodiment uses a mapping function of the ReLU (Rectified Linear Unit) form: ; In the formula, when the topic cohesion When (i.e., when historical semantics are orthogonal or irrelevant), the time decay factor Automatically resets to zero, enabling rapid forgetting of irrelevant history; when When it approaches 1, Approaching 1 preserves high-value context.
[0037] Specifically, for example, in this embodiment 1, the following is set (A review of the last two rounds of history) , Assume the scenario is as follows: (First two rounds): "I want to go to Shanghai on a business trip." (First round): "Book me a plane ticket for next Monday." (Current): "Check the weather over there again." At this point, simply looking at the currently entered phrase "the weather over there" is unclear. Calculation using a formula is needed: ; This step uses mathematical weighting to make... The implicit inclusion of semantic features such as "Shanghai" and "time" eliminates the need for users to repeatedly input the location, thus improving the accuracy of tool recall.
[0038] It is important to note that when calculating the time decay factor... At that time, the system is configured with initialization protection logic: When the number of historical dialogue rounds At this time, since there are no adjacent dialogue pairs, topic cohesion cannot be calculated. At this point, the system skips. The dynamic calculation steps directly... Set to the default value (e.g.) The default value does not affect the final vector calculation result and is mainly used to ensure the integrity of the sequence process and prevent division by zero anomalies.
[0039] In this embodiment 1, the system will finally calculate the hybrid query vector. This will be established as the core input benchmark for subsequent tool retrieval and scoring, replacing the single vector generated solely from the current user's text. For example, the system will use the aforementioned calculated vector, which implicitly contains the geographical features of "Shanghai" and the time feature of "next Monday," as the core input benchmark. The data is directly passed to subsequent calculations of a multidimensional comprehensive relevance score, which is then compared with the standard feature vector of the weather query tool using cosine similarity, instead of using the original vector that only represents "the weather over there". This step achieves implicit completion of the user's current command, automatically filling in key constraints omitted in natural language dialogue through mathematical weighting. This overcomes the problem of a sharp drop in intent recognition accuracy caused by pronouns (such as "there" or "it") or ellipses in multi-turn interactions, ensuring that the system can still accurately understand the context even when the user's expression is incomplete, thereby improving the robustness of tool recall.
[0040] S3. Calculate the multi-dimensional comprehensive relevance score. The purpose of this step is to establish a multi-dimensional evaluation system that recommends tools not only based on literal meaning or single semantic similarity, but also combines the tool's actual performance (success rate) and resource consumption (token cost) to calculate a comprehensive score for each tool. Specifically, it includes the following sub-steps: S31. Calculate the semantic matching score. : ; In the formula, It is the numerical stability constant; Let be the Euclidean norm of the vector. This formula is used to calculate the mixed query vector. With tool feature vectors The cosine similarity can bridge the semantic gap between natural language and technical documents, ensuring that the selected tools are functionally responsive to user requests. This solves the problems of ambiguity and polysemy in natural language instructions and guarantees the basic relevance of the candidate tool set.
[0041] S32, Calculation Tools Historical call success rate : ; In the formula, This is a preset historical statistical sliding time window; In the time window Inside, tools Total number of times the large model is called; In the time window Inside, tools The number of times a task is successfully executed. The formula uses Laplace smoothing (adding 1 to the numerator and 2 to the denominator) to address the cold start problem. For newly added tools that haven't yet been called, to avoid their success rate being directly 0 or encountering division-by-zero errors, they are given an initial neutral probability (0.5), giving them a chance to be explored and called. Semantic scores are corrected using historical performance data, automatically downgrading tools that are theoretically feasible but frequently fail, thereby improving the overall robustness of the system and the task execution success rate.
[0042] S33, Calculation Tools Complexity penalty term :
[0043] In the formula, The length of the token in the tool schema. and These represent the maximum and minimum token consumption amounts in the current toolset, respectively. is a numerical stability constant. This formula assesses the resource consumption cost of the tool by introducing a cost control mechanism. In the MCP architecture, the schema complexity of different tools varies greatly; some tools require only a few parameters, while others contain complex nested structures and long text descriptions. Filling the LLM context window with a large amount of complex tool information consumes a large number of tokens, increasing inference costs and crowding out dialogue history space. Therefore, this method maps the number of tokens to the dimensionless interval [0,1] through max-min normalization, ensuring that it can be weighted at the same order of magnitude as semantic score and success rate score; and introduces a complexity penalty term. When two tools have similar functions (semantic scores are close), the system will prioritize recommending the lightweight tool that consumes less tokens. This achieves dynamic saving of context window resources and reduces inference latency for large models.
[0044] S34. Calculate each tool using a weighted summation method. Overall rating The formula is as follows: ; In the formula, For tools The semantic matching score; For tools Historical call success rate; For tools Complexity penalty term; These are the semantic item weights, historical performance item weights, and complexity penalty item weights, respectively, and they satisfy the following conditions: .
[0045] For example, in this embodiment 1, the weights The result is obtained using a constrained optimization algorithm based on pairwise sorting loss, as follows: First, extract from the system's historical logs Group interaction records, constructing the training set For each query record Construct a "positive and negative sample pair" ;in, This is the tool that was actually successfully invoked in this query without any errors (positive sample); These are tools (negative samples) that exist in the query recall list but were not invoked. For each sample pair, the three-dimensional features described in this invention are calculated, and the feature difference vector between the two is also calculated. Among them, the feature vector Defined as: .
[0046] Secondly, the solution for the weights is transformed into a constrained convex optimization problem. The goal is to find a set of weights. The goal is to maximize the classification margin by ensuring that the scores of positive samples are as high as possible compared to the scores of negative samples throughout all historical records. Define the loss function. Add a regularization term to HingeLoss: ; in, Represents the difference between the scores of positive samples and the scores of negative samples (i.e., ); The preset minimum interval hyperparameter is usually taken as... This means that the score of a positive sample must be at least 0.1 points higher than that of a negative sample to be considered acceptable. This is the regularization coefficient, used to prevent overfitting.
[0047] Then, the objective function is solved under the following physical constraints: ; The above equations are solved iteratively using a sequential least squares programming algorithm: Initialization Calculate the gradient And update the weights. After each update, Projecting back into the simplex constraint space, i.e., normalization, ensures the sum is 1 and non-negative. Stop iteration when the loss function converges or the maximum number of iterations is reached, and output the optimal solution. The system repeats the above steps every preset period using newly added data to recalculate and update. .
[0048] Specifically, for example, setting . In response to the request to "check Shanghai weather", the system retrieved two candidate tools: Tool A, Simple Weather API: Semantic Similarity Token length Historical call success rate .
[0049] Tool B, Complex Meteorological Research Database: While it can also check the weather, the parameters are extremely complex. Semantic similarity. (Slightly lower, as the description focuses on scientific research), Token length Historical success rate .
[0050] Score for Calculation Tool A: Normalized complexity penalty term Tool A Overall Score ; Score for Calculation Tool B: Normalized complexity penalty term Tool B Overall Score ; In the example above, although tool B is not much different from tool A in terms of semantic similarity (0.90 vs 0.92), its final score is significantly lower than tool A due to its huge token consumption (complexity penalty term as high as 0.160). This step, by introducing a complexity penalty term and historical success rate, prioritizes tools with low token consumption and high stability based on semantic similarity, avoiding the selection of overly bloated tools in the model.
[0051] S4. To avoid the inflexibility of fixing the Top-N (e.g., forcibly selecting the top 5), a dynamic threshold based on the current rating distribution is used for filtering. Specifically, the system calculates the statistical characteristics of the overall ratings of all tools and sets a dynamic filtering threshold. : ; In the formula, The arithmetic mean of the scores for all current tools; The standard deviation of the scores for all tools; This is the sensitivity adjustment coefficient.
[0052] The overall tool score calculated in step S3 and In comparison, if If it is selected, it will be marked as a candidate tool. If so, then remove it.
[0053] For example, in this embodiment 1, the sensitivity adjustment coefficient The threshold coefficient is calculated using a probabilistic inverse method based on the maximum capacity. This method does not rely on any artificial weights; instead, it derives the statistically truncated threshold coefficient based on how many tools the remaining space can physically accommodate. The specific calculation formula is as follows: ; ; In the formula, The number of remaining available tokens; To provide a safety margin for system prompts and user commands; This is a truncation function, and the truncation range is... This ensures that even with a very large space, calculation errors will not occur. Negative values allow most tools to pass through, and even if the space is 0, a very small probability is retained. (For large positive values, only the highest score tool passes); among which It is a preset, extremely small positive number used to prevent probability boundary overflow; The maximum context window limit for the currently invoked LLM model; This represents the total number of tokens currently in use during the session. This represents the total number of candidate tools currently participating in the screening process. The average token length of all tools currently participating in the screening; This is the inverse cumulative distribution function of the standard normal distribution. This formula allows the system to automatically adjust the filtering threshold based on the current congestion level of the conversation: in the early stages of the conversation, when tokens are plentiful... The value automatically decreases, allowing more tools to enter the context and provide a wider range of options; in the later stages of multi-turn conversations, when token resources are exhausted, The value automatically increases, allowing only critical tools with extremely high scores to enter. This design ensures that the screening mechanism is always at the optimal balance point allowed by physical resources.
[0054] Specifically, for example, setting Assuming that the calculated average rating of the 100 tools... Standard deviation Then the dynamic threshold Based on the calculation results of S33: Tool A's overall score Selected; Tool B score , was selected.
[0055] In this embodiment 1, the mean and standard deviation of the scores are used to adaptively determine the screening boundary, avoiding omissions or redundancy caused by fixed-quantity truncation. For example, in the above calculation, although the absolute score of tool B (0.555) is significantly lower than that of tool A (0.739), it is still higher than the threshold (0.45) calculated based on the current population statistical characteristics. Therefore, the system determines that it still belongs to the significantly relevant category in the current distribution, and thus retains it together with tool A into the candidate pool. This step breaks the rigid limitation of the traditional Top-K truncation algorithm by constructing an elastic screening mechanism based on relative distribution quality rather than absolute quantity. It realizes the adaptive scaling of the screening strategy with data features. It can retain more options to prevent the omission of key tools when the scores of all candidate tools are generally high (avoiding omissions), and decisively remove all substandard long-tail noise when the scores are generally low (avoiding redundancy). Thus, while ensuring high recall, it maximizes the purification of the context environment input to the large model.
[0056] S5. Construct adaptive prompts and execute LLM calls. This step adaptively determines whether to enable tool call mode based on the filtering results of step S4, and specifically includes the following sub-steps: S51. Based on the screening results of step S4, construct a candidate tool set. and examine the candidate tool set Is it empty?
[0057] S52. If the set is not empty ( Enter enhanced tool mode: system only traverses The candidate tools are converted into JSON description objects conforming to the MCP protocol to construct a streamlined tool list. Because a large number of low-scoring tools are removed, the resulting tool list text is much shorter than the original full tool set, ensuring that the context window primarily carries core user commands rather than irrelevant tool definition noise. At this point, the system-generated prompts include: role definition and context history. The system then streamlines the tool list and user commands. The system then uses the hybrid query vector generated in step S2. The corresponding original natural language text, and the context history obtained in step S13 The system then assembles a streamlined list of tools to create the final system prompt. Finally, the system sends this prompt to the LLM (Linux Virtual Machine). Upon receiving the streamlined and highly relevant tool list, the LLM performs reasoning and decision-making, outputting the final target tool invocation command or directly replying to the user.
[0058] S53. If the set is empty ( Entering pure dialogue mode: The system determines that the current user's intent has extremely low relevance to the existing tool library. To avoid the model developing tool-calling illusions due to interference from irrelevant tools, the system does not load any tool descriptions. At this time, the prompts constructed by the system only include: role definition and context history. User commands. Execution: The system directly calls the large language model, utilizing the model's own pre-trained knowledge base to directly respond to or clarify user commands, without attempting to trigger any external tools.
[0059] In this embodiment 1, prompt words are adaptively constructed based on dynamic filtering results. Only a subset of high-value tools selected through multi-dimensional evaluation is presented to the LLM, solving the context window overload problem caused by the full tool injection strategy in existing technologies. Token resource allocation is optimized by precisely delivering relevant tool definitions. For example, in step S4, the system selects a simple weather API (tool A) and a complex meteorological research library (tool B) as candidate tools. Then, in step S5, only the JSON descriptions of these two tools are assembled into a concise list for injection prompt words. Compared to the original full descriptions of 100 tools (approximately tens of thousands of tokens), this injection only occupies a few hundred tokens, freeing up a significant amount of space in the context window to accommodate user history dialogues and task commands. The LLM receives a cleaned, denoised, and focused list of tools, enabling it to quickly identify which tool A should be called to respond to the "query Shanghai weather" request and output accurate function call parameters. This method reduces inference latency and computational costs caused by long contexts. Furthermore, due to the significant reduction in context noise, the LLM can focus more on understanding core commands and matching tools, thereby improving tool call accuracy and overall system response efficiency. Example 2
[0060] As a second embodiment of the present invention, such as Figure 2 As shown in Example 1, this example also discloses an MCP tool invocation system based on a dynamically filtered large language model, which specifically includes: a feature extraction module, a context fusion module, a scoring module, a dynamic filtering module, and an execution invocation module.
[0061] The feature extraction module is used to parse all the tools exposed by the MCP server and extract the features of each tool. Functional description and parameter schema information, and utilize pre-trained embedding models to integrate each tool Functional description Transform into static feature vectors The data is stored in a vector database; simultaneously, the user's current text input is obtained in real time. and the history dialogue record in the history dialogue window .
[0062] Context fusion module, used to integrate text based on the user's current input. Historical Dialogue Records and preset time decay factor Construct a hybrid query vector that incorporates contextual information. The query vector, which incorporates contextual information, is calculated to capture semantic coherence and implicit user intent in multi-turn dialogues.
[0063] The scoring module is used to calculate the score for each tool. Semantic matching score relative to mixed query vectors and obtain each tool Historical call success rate Simultaneously calculate each tool Complexity penalty term Comprehensive calculation of each tool Overall rating .
[0064] The dynamic filtering module is used to provide a comprehensive score based on all tools. Based on the statistical characteristics and the remaining space of the current context window, a dynamic filtering threshold is calculated. Each tool Overall rating With dynamic filtering thresholds Comparison and selection of candidate tool sets .
[0065] The execution module is used to adaptively construct system suggestion words based on the output of the dynamic filtering module: if the candidate tool set If not empty, the candidate tool will be converted into a JSON description object conforming to the MCP protocol, and linked with the role definition, user commands, and context history dialogue records. The system prompts are assembled into a concise list of tools; if the candidate tool set is empty, a pure dialogue prompt without any tool description information is constructed; finally, the constructed prompts are sent to the large language model for inference invocation or direct dialogue response.
[0066] In the implementation of Implementation 2 described above, the various modules of the system are connected sequentially and work together to form a complete dynamic filtering and invocation pipeline. Specifically: the feature extraction module first preprocesses the tool library and captures user input in real time; the context fusion module generates context-rich query vectors; the scoring module quantitatively evaluates the tools based on multi-dimensional indicators; the dynamic filtering module flexibly determines the selected tools based on resource availability and scoring distribution; and the execution invocation module finally completes the construction of prompt words and model invocation, thereby achieving efficient, accurate, and resource-adaptive MCP tool invocation.
[0067] For example, suppose the system connects to 100 MCP tools. The user's current input is "Check the weather in Beijing tomorrow," and the historical dialogue includes "I'm going to Beijing on a business trip." The feature extraction module acquires this input and historical records in real time and converts the tool descriptions of weather queries, stock queries, etc., from the tool library into static vectors. The context fusion module combines the current input and historical dialogues to generate a hybrid query vector that implicitly contains key information such as "Beijing" and "tomorrow." The scoring module calculates the semantic similarity of all tools to this vector, their respective historical success rates, and schema complexity, and obtains a comprehensive score for each tool. The dynamic filtering module calculates the mean and standard deviation of all current ratings, and determines the filtering threshold by considering the number of tokens already used in the current dialogue and the upper limit of the model window. Only tools with scores above a threshold (such as the Simple Weather API and Life Index queries) are retained. The execution module converts the selected few highly relevant tools into JSON format, assembles them into concise prompts, and sends them to the LLM. The LLM then accurately calls the weather query tool and returns the results. This entire process avoids injecting all 100 tool descriptions into the context, significantly saving token resources and improving call accuracy and response speed.
[0068] Furthermore, to better implement the above-described embodiments, Embodiment 2 of the present invention provides a process for obtaining a pre-trained Embedding model, used for each tool description. Transform into static feature vectors Specifically, it includes the following steps: A1. Constructing a structured input sequence. Unlike plain text, MCP tool descriptions contain structured information. The model first... The information of each tool is serialized into a specific token sequence. : ; in, This is the semantic aggregation vector bit for this sequence; These represent the tool name, function description, and parameter list, respectively. This indicates a sequence concatenation operation.
[0069] A2, Multi-head Self-Attention Encoding. (The sequence is...) Input the Transformer encoder layer. To capture the importance of different keywords (such as action words like "query" and "delete") in the tool description, compute multi-head self-attention. For the The first layer The formula for calculating the attention points is: ; Among them, are query, key, and value matrices respectively, which is the output of the previous layer; is the dimension of the key vector, which is used as a scaling factor to prevent the vanishing gradient caused by an excessive dot product and ensures the stability of numerical calculations.
[0070] A3. Pooling and output of eigenvectors. After layers of encoding, the hidden state of the last layer is extracted . To obtain a fixed-length static eigenvector , a weighted average pooling strategy is adopted instead of just taking to retain more parameter details: ; Among them, is the output vector of the th Token in the sequence at the last layer; is a preset weight based on the token's词性 (e.g., verbs and nouns have higher weights); is the L2 normalization function to ensure , facilitating subsequent cosine similarity calculations.
[0071] Exemplarily, the present embodiment adopts the following non-linear weight assignment strategy: Verbs (Verbs, e.g., v, vn): Represent the core functions of the tool (such as "query", "generate", "delete"), and the weight is set to ; Nouns (Nouns, e.g., n, nr, ns): Represent the objects or entities of the operation (such as "weather", "stock", "database"), and the weight is set to ; Parameter keywords (Args): The keyword field names defined in the tool parameter list, and the weight is set to ; Adjectives / Adverbs (Adj / Adv): Used to modify the limiting conditions of the function, and the weight is set to ; Others: Include prepositions, auxiliary words, punctuation marks, and stop words (such as "de", "le", "shi"), and the weight is set to ; Special Tokens ([CLS], [SEP]): To maintain the overall coherence of the sequence, the weight is set to the default value .
[0072] A4. Comparative learning training based on the InfoNCE loss function. To enable the model to learn to match user intent, supervised comparative learning is used for fine-tuning. Positive sample pairs are constructed. (Tools for user queries and correct invocation) and negative sample pairs (User queries and irrelevant tools). Define the training loss function. for: ; in, This represents the cosine similarity of vectors (since it has been normalized, the dot product is the cosine similarity). This is a temperature coefficient used to control the degree to which the model pays attention to difficult negative samples; The negative sample set consists of other tools within the same batch.
[0073] A41. In this embodiment, the system lacked real user historical call logs in the early stages of development (i.e., lacked real...). To address the cold start problem of tags, this embodiment employs LLM reverse generation and data synthesis techniques to construct the initial training set. The specific implementation steps are as follows: First, a more powerful general-purpose large language model (TeacherLLM, such as GPT-4) is used as the data generator. The system iterates through each tool parsed in step S11. Describe its functions The parameter Schema is input as a prompt word to TeacherLLM, which is then tasked with simulating different user styles and generating natural language queries that can trigger the tool. .
[0074] Secondly, in order to simulate the complexity of real-world environments, diversity constraints are set in the generation of prompts, requiring that they be specific to the same tool. Generate three types of queries: Explicit instructions: clear intent, containing core keywords (e.g., "Check the weather in Beijing for me"); Implicit intention: vague expression, context-dependent (e.g., "I'm going to Shanghai tomorrow, do I need to bring an umbrella?"). Complex parameters: Includes multiple parameter restrictions (e.g., "Find Sichuan restaurants with a rating greater than 4.0 and a price under 200 yuan").
[0075] Finally, the generated synthetic query Its corresponding source tools Forming positive sample pairs For each tool, generate (For example, 50) synthetic instructions are used to construct the initial cold start training set: ; It should be noted that publicly available, general-purpose tool call datasets (such as ToolBench and API-Bank) are used to extract data. The model undergoes hybrid training. This allows the embedding model to first learn the general rules for matching user commands with API functions, and then, through the aforementioned synthetic data... Learn the domain knowledge of the specific tools in this system.
[0076] A42. To avoid the model relying solely on coarse-grained semantic features (such as only identifying query-type tools) for differentiation, this embodiment abandons the traditional global random sampling when constructing the training batch. Instead, it adopts an anchor-hard sample hybrid sampling strategy. The specific steps are as follows: First, before training begins, the system first calculates the toolset based on the BM25 algorithm. The text similarity between each pair of tools is used to construct a similarity matrix. For each tool Select the top ones with the highest similarity scores Non-self tools (e.g.) This serves as the "hard negative sample candidate set" for the tool. Using BM25 based on literal matching instead of semantic vectors is to prevent model collapse, that is, to avoid the model using its own erroneous bias to select negative samples, and to ensure that negative samples are indeed highly confusing in the literal sense of the text.
[0077] Secondly, in constructing each training batch (size: When sampling is performed, stratified sampling should be conducted according to the following proportions: Anchor sampling (50%): Randomly sampled from the dataset Positive sample pairs As anchor data.
[0078] Accompanied by difficult sampling (50%): for each of the above anchor points, the tool From its corresponding difficult negative sample candidate set Randomly select one tool The system automatically searches for entries containing this. Positive sample pairs Add to the current batch.
[0079] The dynamic effect of negative samples within a batch is achieved through the above construction method, generating a batch. Logically, this creates multiple miniature "confusion groups." When calculating the InfoNCE loss function, for any sample within a batch... : In the denominator It will automatically include deliberately introduced difficult negative samples. .
[0080] because and The functions are very similar (e.g., "query the current weather" and "query the future weather"). In order to reduce the loss, the model must force the attention mechanism to pay attention to the subtle differences in the description (e.g., the tokens for "current" vs. "future"), so as to learn more discriminative fine-grained features.
[0081] Through the above steps, the Embedding model trained in this embodiment can bring semantically related but literally mismatched words (such as the user input "I want to buy tickets" and the tool description "flight booking service") closer together in the vector space, thereby improving the accuracy of vector calculation.
[0082] In this embodiment, the Transformer encoder layer adopts a standard BERT-based model architecture configuration. Specific parameter settings are as follows: number of encoder stacking layers. The dimension of the hidden state vector Number of heads in a multi-head self-attention mechanism And the key vector dimension of each attention head. The following dimensional relationship must be satisfied: At this point, the scaling factor This effectively prevents the vanishing Softmax gradient problem caused by excessively large dot product results, ensuring the numerical stability of model training.
[0083] Model initialization and training strategy: This embodiment is based on an open-source, general-purpose pre-trained language model (such as BERT-base-Chinese) for initialization. The specific training process consists of two stages: General pre-training stage: The model parameters, which have been pre-trained on a large-scale general corpus, are directly loaded to enable the model to have basic Chinese semantic understanding and syntactic analysis capabilities.
[0084] Domain adaptation fine-tuning stage (i.e., step A4 above): Keep the main model architecture unchanged, use the constructed MCP tool to perform full fine-tuning or parameter efficient fine-tuning (PEFT) on the model parameters through the InfoNCE loss function on the positive and negative sample pairs dataset.
[0085] In the description of this specification, references to terms such as "an embodiment," "example," "specific example," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.
[0086] The preferred embodiments of the invention disclosed above are merely illustrative of the invention. These preferred embodiments do not exhaustively describe all details, nor do they limit the invention to the specific implementations described. Clearly, many modifications and variations can be made based on the content of this specification. This specification selects and specifically describes these embodiments to better explain the principles and practical applications of the invention, thereby enabling those skilled in the art to better understand and utilize the invention.
Claims
1. A method for calling MCP tools based on dynamic filtering of large language models, characterized in that, include: Analyze all tools exposed by the MCP server, extract the functional description and parameter schema information of each tool, and use the Embedding model to transform the functional description of each tool into a static feature vector; obtain the user's current text input and the historical dialogue records in the historical dialogue window in real time; Based on the user's current text input and historical dialogue records, and by introducing a time decay factor, a hybrid query vector that incorporates contextual information is constructed. Calculate the semantic matching score between the hybrid query vector and the static feature vector of each tool, obtain the historical call success rate of each tool, calculate the schema complexity penalty term for each tool, and calculate the comprehensive score of each tool by weighted summation; Calculate the statistical characteristics of the overall scores of all current tools, and calculate a dynamic filtering threshold based on the statistical characteristics and the remaining space of the current context window; compare the overall score of each tool with the dynamic filtering threshold to filter out a set of candidate tools. Based on the selected candidate tool set, a system prompt word containing a concise tool list is constructed and sent to the large language model for inference invocation; if the candidate tool set is empty, a system prompt word without a tool list is constructed for pure dialogue response.
2. The method for calling MCP tools based on dynamically filtered large language models according to claim 1, characterized in that, The specific formula for the hybrid query vector is: ; In the formula, Represents a mixed query vector. Represents a vector embedding function. This indicates the user's current text input. Indicates the first The content of the historical dialogue of the wheel, This represents the weight coefficient of the current input. The time decay factor representing historical information.
3. The method for calling the MCP tool based on dynamically filtered large language models according to claim 2, characterized in that, The formula for calculating the overall score of each tool is as follows: ; In the formula, For tools Overall score For tools The semantic matching score, For tools Historical call success rate, For tools Complexity penalty term, , , These are the semantic item weights, historical performance item weights, and complexity penalty item weights, respectively, and they satisfy the following conditions: .
4. The method for calling MCP tools based on dynamically filtered large language models according to claim 3, characterized in that, The tool semantic matching score By calculating the mixed query vector With tool static feature vector The cosine similarity is obtained.
5. The method for calling the MCP tool based on dynamically filtered large language models according to claim 3, characterized in that, The tool Historical call success rate The result is calculated based on the number of successful calls within a preset time window and the total number of calls, and then smoothed using Laplace's algorithm.
6. The method for calling MCP tools based on dynamically filtered large language models according to claim 3, characterized in that, The tool Complexity penalty term Token length based on tool schema Maximum value in the current toolset and minimum value Obtained by performing max-min normalization calculation.
7. The method for calling MCP tools based on dynamically filtered large language models according to claim 3, characterized in that, The formula for calculating the dynamic screening threshold is: ; In the formula, As the filtering threshold, The arithmetic mean of the scores for all current tools. The standard deviation of the scores for all tools This is the sensitivity adjustment coefficient; If tools Overall rating If it is selected, it will be included in the candidate tool set; if If so, then remove it.
8. The method for calling MCP tools based on dynamically filtered large language models according to claim 7, characterized in that, The formula for calculating the sensitivity adjustment coefficient is as follows: ; In the formula, The number of remaining available tokens; This is a truncation function, and the truncation range is... , It is a preset minimum positive number; The maximum context window limit for the currently invoked LLM model. This represents the total number of tokens currently in use during the session. This represents the total number of candidate tools currently participating in the screening process. The average token length of all tools currently participating in the screening. It is the inverse cumulative distribution function of the standard normal distribution.
9. The method for calling MCP tools based on dynamically filtered large language models according to claim 1, characterized in that, The construction of the system prompt includes: if the candidate tool set is not empty, it is converted into a JSON description object conforming to the MCP protocol, and assembled into a system prompt together with the role definition, user command, and context history dialogue record; if the candidate tool set is empty, the system prompt does not contain any tool description information.
10. A system for calling MCP tools based on dynamically filtered large language models, employing the MCP tool calling method based on dynamically filtered large language models as described in any one of claims 1 to 9, characterized in that, include: The feature extraction module is used to parse the MCP toolset, extract the functional descriptions of the tools and convert them into static feature vectors to be stored in the vector database, and obtain the user's current text input and historical dialogue records in real time. The context fusion module is used to calculate a context-aware hybrid query vector based on the user's current input, historical dialogue records, and time decay factor. The scoring module is used to calculate the semantic matching score, historical call success rate, and complexity penalty for each tool relative to the mixed query vector, and to comprehensively calculate the multi-dimensional comprehensive relevance score for each tool. The dynamic filtering module is used to calculate a dynamic filtering threshold based on the statistical characteristics of the comprehensive scores of all tools and the remaining space in the current context window, and to filter out a set of candidate tools accordingly. The execution module is used to adaptively construct system prompts that include or exclude a simplified tool list based on the filtering results, and invoke a large language model for reasoning or dialogue response.