Close Menu
    Trending
    • Become a Better Data Scientist with These Prompt Engineering Tips and Tricks
    • Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025
    • Transform Complexity into Opportunity with Digital Engineering
    • OpenAI Is Fighting Back Against Meta Poaching AI Talent
    • Lessons Learned After 6.5 Years Of Machine Learning
    • Handling Big Git Repos in AI Development | by Rajarshi Karmakar | Jul, 2025
    • National Lab’s Machine Learning Project to Advance Seismic Monitoring Across Energy Industries
    • HP’s PCFax: Sustainability Via Re-using Used PCs
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»How Not to Write an MCP Server
    Artificial Intelligence

    How Not to Write an MCP Server

    Team_AIBS NewsBy Team_AIBS NewsMay 9, 2025No Comments13 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    I the possibility to create an MCP server for an observability utility with a view to present the AI agent with dynamic code evaluation capabilities. Due to its potential to remodel purposes, MCP is a know-how I’m much more ecstatic about than I initially was about genAI basically. I wrote extra about that and a few intro to MCPs basically in a earlier post.

    Whereas an preliminary POCs demonstrated that there was an immense potential for this to be a pressure multiplier to our product’s worth, it took a number of iterations and a number of other stumbles to ship on that promise. On this submit, I’ll attempt to seize among the classes realized, as I believe that this may profit different MCP server builders.

    My Stack

    • I used to be utilizing Cursor and vscode intermittently as the principle MCP consumer
    • To develop the MCP server itself, I used the .NET MCP SDK, as I made a decision to host the server on one other service written in .NET

    Lesson 1: Don’t dump your entire knowledge on the agent

    In my utility, one device returns aggregated data on errors and exceptions. The API may be very detailed because it serves a posh UI view, and spews out giant quantities of deeply linked knowledge:

    • Error frames
    • Affected endpoints
    • Stack traces 
    • Precedence and traits 
    • Histograms

    My first hunch was to easily expose the API as is as an MCP device. In spite of everything, the agent ought to be capable to make extra sense of it than any UI view, and catch on to attention-grabbing particulars or connections between occasions. There have been a number of eventualities I had in thoughts as to how I’d anticipate this knowledge to be helpful. The agent may mechanically provide fixes for current exceptions recorded in manufacturing or within the testing atmosphere, let me find out about errors that stand out, or assist me handle some systematic issues which are the underlying root reason behind the problems. 

    The essential premise was due to this fact to permit the agent to work its ‘magic’, with extra knowledge probably which means extra hooks for the agent to latch on in its investigation efforts. I rapidly coded a wrapper round our API on the MCP endpoint and determined to begin with a fundamental immediate to see whether or not every little thing is working:

    Picture by creator

    We will see the agent was good sufficient to know that it wanted to name one other device to seize the atmosphere ID for that ‘take a look at’ atmosphere I discussed. With that at hand, after discovering that there was truly no current exception within the final 24 hours, it then took the freedom to scan a extra prolonged time interval, and that is when issues bought a little bit bizarre:

    Picture by creator

    What a wierd response. The agent queries for exceptions from the final seven days, will get again some tangible outcomes this time, and but proceeds to ramble on as if ignoring the info altogether. It continues to attempt to use the device in several methods and completely different parameter combos, clearly fumbling, till I discover it flat out calls out the truth that the info is totally invisible to it. Whereas errors are being despatched again within the response, the agent truly claims there are no errors. What’s going on?

    Picture by creator

    After some investigation, the issue was revealed to be the truth that we’ve merely reached a cap within the agent’s capability to course of giant quantities of knowledge within the response.

    I used an present API that was extraordinarily verbose, which I initially even thought of to be a bonus. The top end result, nonetheless, was that I by some means managed to overwhelm the mannequin. General, there have been round 360k characters and 16k phrases within the response JSON. This contains name stacks, error frames, and references. This ought to have been supported simply by wanting on the context window restrict for the mannequin I used to be utilizing (Claude 3.7 Sonnet ought to help as much as 200k tokens), however however the massive knowledge dump left the agent totally stumped.

    One technique can be to alter the mannequin to at least one that helps a fair larger context window. I converted to the Gemini 2.5 professional mannequin simply to check that principle out, because it boasts an outrageous restrict of 1 million tokens. Certain sufficient, the identical question now yielded a way more clever response:

    Picture by creator

    That is nice! The agent was capable of parse the errors and discover the systematic reason behind lots of them with some fundamental reasoning. Nevertheless, we are able to’t depend on the person utilizing a particular mannequin, and to complicate issues, this was output from a comparatively low bandwidth testing atmosphere. What if the dataset have been even bigger? 
    To resolve this situation, I made some elementary modifications to how the API was structured:

    • Nested knowledge hierarchy: Maintain the preliminary response targeted on high-level particulars and aggregations. Create a separate API to retrieve the decision stacks of particular frames as wanted. 
    • Improve queryability: The entire queries made to date by the agent used a really small web page dimension for the info (10), if we wish the agent to have the ability to to entry extra related subsets of the info to suit with the restrictions of its context, we have to present extra APIs to question errors based mostly on completely different dimensions, for instance: affected strategies, error sort, precedence and affect and so forth. 

    With the brand new modifications, the device now constantly analyzes necessary new exceptions and comes up with repair ideas. Nevertheless, I glanced over one other minor element I wanted to kind earlier than I may actually use it reliably.

    Lesson 2: What’s the time?

    Picture generated by the creator with Midjourney

    The keen-eyed reader might have observed that within the earlier instance, to retrieve the errors in a particular time vary, the agent makes use of the ISO 8601 time length format as an alternative of the particular dates and instances. So as an alternative of together with customary ‘From’ and ‘To’ parameters with datetime values, the AI despatched a length worth, for instance, seven days or P7D, to point it needs to verify for errors prior to now week.

    The explanation for that is considerably unusual — the agent may not know the present date and time! You possibly can confirm that your self by asking the agent that straightforward query. The beneath would have made sense have been it not for the truth that I typed that immediate in at round midday on Might 4th…

    Picture by creator

    Utilizing time length values turned out to be an awesome answer that the agent dealt with fairly effectively. Don’t neglect to doc the anticipated worth and instance syntax within the device parameter description, although!

    Lesson 3: When the agent makes a mistake, present it tips on how to do higher

    Within the first instance, I used to be truly shocked by how the agent was capable of decipher the dependencies between the completely different device calls So as to present the fitting atmosphere identifier. In learning the MCP contract, it found out that it needed to name on a dependent one other device to get the checklist of atmosphere IDs first.

    Nevertheless, responding to different requests, the agent would typically take the atmosphere names talked about within the immediate verbatim. For instance, I observed that in response to this query: evaluate gradual traces for this methodology between the take a look at and prod environments, are there any vital variations? Relying on the context, the agent would typically use the atmosphere names talked about within the request and would ship the strings “take a look at” and “prod” because the atmosphere ID. 

    In my unique implementation, my MCP server would silently fail on this state of affairs, returning an empty response. The agent, upon receiving no knowledge or a generic error, would merely give up and attempt to clear up the request utilizing one other technique. To offset that habits, I rapidly modified my implementation in order that if an incorrect worth was supplied, the JSON response would describe precisely what went improper, and even present a legitimate checklist of doable values to save lots of the agent one other device name.

    Picture by creator

    This was sufficient for the agent, studying from its mistake, it repeated the decision with the proper worth and by some means additionally prevented making that very same error sooner or later.

    Lesson 4: Concentrate on person intent and never performance

    Whereas it’s tempting to easily describe what the API is doing, typically the generic phrases don’t fairly enable the agent to appreciate the kind of necessities for which this performance would possibly apply greatest. 

    Let’s take a easy instance: My MCP server has a device that, for every methodology, endpoint, or code location, can point out the way it’s getting used at runtime. Particularly, it makes use of the tracing knowledge to point which utility flows attain the precise operate or methodology.

    The unique documentation merely described this performance:

    [McpServerTool,
    Description(
    @"For this method, see which runtime flows in the application
    (including other microservices and code not in this project)
    use this function or method.
    This data is based on analyzing distributed tracing.")]
    public static async Job GetUsagesForMethod(IMcpService consumer,
    [Description("The environment id to check for usages")]
    string environmentId,
    [Description("The name of the class. Provide only the class name without the namespace prefix.")]
    string codeClass,
    [Description("The name of the method to check, must specify a specific method to check")]
    string codeMethod)

    The above represents a functionally correct description of what this device does, nevertheless it doesn’t essentially make it clear what kinds of actions it may be related for. After seeing that the agent wasn’t selecting this device up for varied prompts I believed it could be pretty helpful for, I made a decision to rewrite the device description, this time emphasizing the use circumstances:

    [McpServerTool,
    Description(
    @"Find out what is the how a specific code location is being used and by
    which other services/code.
    Useful in order to detect possible breaking changes, to check whether
    the generated code will fit the current usages,
    to generate tests based on the runtime usage of this method,
    or to check for related issues on the endpoints triggering this code
    after any change to ensure it didnt impact it"

    Updating the text helped the agent realize why the information was useful. For example, before making this change, the agent would not even trigger the tool in response to a prompt similar to the one below. Now, it has become completely seamless, without the user having to directly mention that this tool should be used:

    Image by author

    Lesson 5: Document your JSON responses

    The JSON standard, at least officially, does not support comments. That means that if the JSON is all the agent has to go on, it might be missing some clues about the context of the data you’re returning. For example, in my aggregated error response, I returned the following score object:

    "Score": {"Score":21,
    "ScoreParams":{ "Occurrences":1,
    "Trend":0,
    "Recent":20,
    "Unhandled":0,
    "Unexpected":0}}

    Without proper documentation, any non-clairvoyant agent would be hard pressed to make sense of what these numbers mean. Thankfully, it is easy to add a comment element at the beginning of the JSON file with additional information about the data provided:

    "_comment": "Each error contains a link to the error trace,
    which can be retrieved using the GetTrace tool,
    information about the affected endpoints the code and the
    relevant stacktrace.
    Each error in the list represents numerous instances
    of the same error and is given a score after its been
    prioritized.
    The score reflects the criticality of the error.
    The number is between 0 and 100 and is comprised of several
    parameters, each can contribute to the error criticality,
    all are normalized in relation to the system
    and the other methods.
    The score parameters value represents its contributation to the
    overall score, they include:
    
    1. 'Occurrences', representing the number of instances of this error
    compared to others.
    2. 'Trend' whether this error is escalating in its
    frequency.
    3. 'Unhandled' represents whether this error is caught
    internally or poropagates all the way
    out of the endpoint scope
    4. 'Unexpected' are errors that are in high probability
    bugs, for example NullPointerExcetion or
    KeyNotFound",
    "EnvironmentErrors":[]

    This allows the agent to elucidate to the person what the rating means in the event that they ask, but in addition feed this rationalization into its personal reasoning and proposals.

    Selecting the best structure: SSE vs STDIO,

    There are two architectures you need to use in creating an MCP server. The extra widespread and extensively supported implementation is making your server obtainable as a command triggered by the MCP consumer. This may very well be any CLI-triggered command; npx, docker, and python are some widespread examples. On this configuration, all communication is finished through the method STDIO, and the method itself is operating on the consumer machine. The consumer is liable for instantiating and sustaining the lifecycle of the MCP server.

    Picture by creator

    This client-side structure has one main downside from my perspective: For the reason that MCP server implementation is run by the consumer on the native machine, it’s a lot more durable to roll out updates or new capabilities. Even when that drawback is by some means solved, the tight coupling between the MCP server and the backend APIs it is dependent upon in our purposes would additional complicate this mannequin when it comes to versioning and ahead/backward compatibility.

    For these causes, I selected the second sort of MCP Server — an SSE Server hosted as part of our utility companies. This removes any friction from operating CLI instructions on the consumer machine, in addition to permits me to replace and model the MCP server code together with the applying code that it consumes. On this state of affairs, the consumer is supplied with a URL of the SSE endpoint with which it interacts. Whereas not all shoppers at present help this feature, there’s a sensible commandMCP referred to as supergateway that can be utilized as a proxy to the SSE server implementation. Which means customers can nonetheless add the extra extensively supported STDIO variant and nonetheless devour the performance hosted in your SSE backend.

    Picture by creator

    MCPs are nonetheless new

    There are various extra classes and nuances to utilizing this deceptively easy know-how. I’ve discovered that there’s a huge hole between implementing a workable MCP to at least one that may truly combine with person wants and utilization eventualities, even past these you’ve anticipated. Hopefully, because the know-how matures, we’ll see extra posts on Best Practices. 

    Need to Join? You possibly can attain me on Twitter at @doppleware or through LinkedIn.
    Comply with my mcp for dynamic code evaluation utilizing observability at https://github.com/digma-ai/digma-mcp-server



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTopluluk Öğrenmesi(Ensemble Learning) | by X | May, 2025
    Next Article Pinterest CEO Says AI Helped Revenue Grow By 16%
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    Become a Better Data Scientist with These Prompt Engineering Tips and Tricks

    July 1, 2025
    Artificial Intelligence

    Lessons Learned After 6.5 Years Of Machine Learning

    July 1, 2025
    Artificial Intelligence

    Prescriptive Modeling Makes Causal Bets – Whether You Know it or Not!

    June 30, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Become a Better Data Scientist with These Prompt Engineering Tips and Tricks

    July 1, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Hey ChatGPT, Quantum AI Optimization with MLLMs

    June 26, 2025

    Sam Altman’s Younger Sister Files Lawsuit Claiming He Sexually Abused Her

    January 8, 2025

    TikTok True Crime to Stream: ‘Dancing for the Devil’ and More

    January 31, 2025
    Our Picks

    Become a Better Data Scientist with These Prompt Engineering Tips and Tricks

    July 1, 2025

    Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025

    July 1, 2025

    Transform Complexity into Opportunity with Digital Engineering

    July 1, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.