<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Ilgayev on Medium]]></title>
        <description><![CDATA[Stories by Alex Ilgayev on Medium]]></description>
        <link>https://medium.com/@alex.ilgayev?source=rss-122bc868a735------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5xeTGTYkrt4VfZln3aB7Dw.png</url>
            <title>Stories by Alex Ilgayev on Medium</title>
            <link>https://medium.com/@alex.ilgayev?source=rss-122bc868a735------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:16:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alex.ilgayev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Monitoring MCP Traffic Using eBPF: Part 1]]></title>
            <link>https://medium.com/@alex.ilgayev/monitoring-mcp-traffic-using-ebpf-part-1-c445b76377cf?source=rss-122bc868a735------2</link>
            <guid isPermaLink="false">https://medium.com/p/c445b76377cf</guid>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[ebpf]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Alex Ilgayev]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 11:16:07 GMT</pubDate>
            <atom:updated>2025-08-18T11:16:07.044Z</atom:updated>
            <content:encoded><![CDATA[<h4>The journey of creating the MCPSpy open-source project to monitor MCP traffic</h4><p>In this first part of the series, I share the motivations and design of <a href="https://github.com/alex-ilgayev/mcpspy"><strong>MCPSpy</strong></a>, an open-source tool I developed for monitoring <a href="https://modelcontextprotocol.io/"><strong>Model Context Protocol (MCP)</strong></a> traffic. We’ll discuss why MCP matters, why I chose eBPF for monitoring it, how MCPSpy’s initial implementation works, and what limitations exist. Future chapters will explore additional features, such as inspecting encrypted HTTPS-based MCP communications over TLS. But first, let’s start with the basics of what MCP is and how MCPSpy came to be.</p><h3>Why Care About MCP?</h3><p><strong>MCP (Model Context Protocol)</strong> is an emerging standard that defines how AI applications (clients) communicate with external tools and data sources (servers). It builds on JSON-RPC 2.0 to exchange messages, defining core primitives like <strong>Tools</strong>, <strong>Resources</strong>, and <strong>Prompts</strong> that servers can expose to AI clients. In other words, MCP provides a <em>unified interface</em> for AI assistants to invoke functions, fetch data, or use predefined prompts from external services.</p><p>The benefit of MCP is that instead of every AI product writing <strong>custom integration </strong>code for each <strong>external service</strong>, they can all speak a <strong>common protocol</strong>. It’s analogous to how different web services standardized on HTTP — you don’t write a custom network stack for GitHub, Slack, and Google Maps individually. You just use HTTP for all. Similarly, AI clients and tools can interact through MCP instead of custom APIs.</p><p><strong>Adoption is growing fast.</strong> Most AI platforms already support MCP. For example, <strong>Claude Desktop</strong>, <strong>GitHub Copilot</strong>, <strong>Cursor</strong>, and many others can act as <strong>MCP clients</strong>, allowing them to connect seamlessly with a variety of <strong>MCP servers </strong>(tools). This rapid adoption leads to MCP becoming the de facto standard for extending AI assistants with third-party capabilities.</p><h4>The ‘S’ in MCP Stands for Security</h4><p>With the pace of AI innovation and MCP’s quick development, there’s a noticeable gap in tooling to <strong>inspect and understand</strong> what these AI-driven tools are actually doing. When an AI assistant uses MCP to perform actions, how do we know what data it’s sending or receiving? Are there potential risks of it doing something unintended or malicious?</p><p>For example, the LLM client (Cursor in this case) asks us before running an MCP command (with GitHub MCP), but until we expand the box and analyze the data, we won’t know what actual data was sent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S9RFrhmx9NGmw3i8n9vJdg.png" /></figure><p>This is a good situation where users are prompted with approval before running, but I truly believe that user consent will be reduced to a minimum, especially in the era of <strong>background AI agents </strong>(like Cursor background agents or Codex by OpenAI).</p><p>Another concern is <strong>detecting anomalous or dangerous tool usage orchestrated by an AI.</strong> Let’s say your assistant uses the <a href="https://github.com/github/github-mcp-server">GitHub MCP server</a>, which currently offers over <strong>80 different tools (!) </strong>for various GitHub operations. Even if you try to lock it down by allowing only certain tools, all it takes is one powerful tool (for instance, a create_or_update_file tool) used improperly to cause damage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vDYtvHdYvEhUfyjPOl7CBg.png" /></figure><p>The AI agent might <strong>hallucinate </strong>or make a mistake and update the <strong>wrong file </strong>or issue a <strong>destructive command</strong>. Worse, if the AI has been given a <strong>strong access token </strong>for the MCP server, <em>nothing technical </em>stops it from abusing that token except our trust that the AI won’t misbehave. We all know AI models can be unpredictable – they don’t intend harm, but they certainly can make unintended moves. If an AI agent decided (or was prompted) to execute a series of harmful tool calls, how would we catch it?</p><p><strong>These challenges drove me to create MCPSpy.</strong> My goal is to build tooling that can <em>iteratively improve our security posture</em> around AI tool use: first by gaining visibility into MCP communications, next by detecting suspicious activity, and eventually by preventing truly malicious actions. <strong>Visibility is the first step</strong> — you can’t secure what you can’t see.</p><p>Before diving into how it works, it’s worth mentioning a few things <strong>MCPSpy does <em>not</em> cover:</strong></p><ul><li><strong>Only Linux is supported:</strong> MCPSpy relies on eBPF, a Linux kernel technology. It cannot yet monitor MCP on Windows or macOS. (There is <a href="https://github.com/microsoft/ebpf-for-windows">work in progress </a>to bring eBPF capabilities to Windows, but it’s currently far from feature parity with Linux)</li><li><strong>Malicious MCP Servers are out of scope:</strong> MCPSpy is about monitoring the <em>protocol&#39;s usage</em>, not about verifying the servers themselves. If you connect to a malicious MCP server (one designed to steal data or run harmful code), that’s a different threat model. The assumption is that you are running trusted MCP servers. Securing them (through code review, sandboxing, or other runtime protections) is another challenge entirely, and not what MCPSpy is meant for.</li><li><strong>Vulnerabilities in MCP servers are out of scope:</strong> Similarly, if an MCP server has a vulnerability (say, an injection that can cause RCE), MCPSpy doesn’t help there.</li></ul><h3>Why eBPF</h3><p>When thinking about all the ways to monitor MCP traffic on a host, you may consider several approaches. Ultimately, I landed on using <strong>eBPF</strong>, but it’s worth understanding <em>why</em> by looking at the alternatives and their drawbacks.</p><h4><strong>LD_PRELOAD hooking</strong></h4><p>The idea is to intercept relevant functions by injecting a shared library into processes via the LD_PRELOAD mechanism. This could, in theory, let us wrap functions like read()/write() or higher-level MCP library calls in user-space.</p><p>However, this approach has significant downsides:</p><ul><li>It requires starting each process you want to monitor with the custom LD_PRELOAD library, which is impractical in many scenarios, especially if you need to attach to processes in a production environment without modifying how they’re launched.</li><li>You are unable to hook into internal kernel probes, which sometimes offer more control and visibility into kernel structures (for example, getting inode numbers).</li><li>It would complicate the development as it needs to be tested against various libc and distributions.</li></ul><h4><strong>Custom Kernel Module</strong></h4><p>Another route was writing a <strong>Linux kernel module</strong> to monitor MCP traffic. While this could certainly work, developing a custom module is a heavy task:</p><ul><li>Steep learning curve, and very risky — one bug can crash the entire system, so you need to be extremely careful and test against many kernel versions.</li><li>It is not trivial to hook into user-mode events, which is needed to monitor encrypted TLS traffic (for remote MCP calls).</li></ul><h4>eBPF</h4><p>If you haven’t been keeping up with Linux developments in the past few years, <a href="https://ebpf.io/"><strong>eBPF</strong></a> (extended Berkeley Packet Filter) has revolutionized how we can <strong>instrument and monitor the kernel</strong>. It allows us to load <strong>small, safe programs into the kernel at runtime</strong>, attaching them to various <strong>hooks </strong>(both kernel and user-space via uprobes).</p><p>The key is that eBPF programs are verified by the kernel <strong>verifier</strong> for safety before they run — they can’t crash the system, and they’re limited in what they can do (no infinite loops, bounded memory access, etc.). Thus, they can be safely used even in production environments.</p><p>To sum up: <strong>eBPF it is.</strong> Now let’s look at how I used eBPF to track MCP traffic and how MCPSpy is implemented under the hood.</p><h3>Implementation Phase</h3><p>We live in the age of AI-assisted coding. In fact, I leaned on <strong>Cursor + Claude Sonnet </strong>to scaffold this project, including generating many boilerplates. Still, <strong>writing eBPF code using AI </strong>can be tricky. There isn’t a lot of codebase the AI can be trained upon, it requires deep knowledge of the Linux codebase, and understanding the cryptic messages of the verifier errors requires expertise. So while you are welcome to write eBPF with code assistants, <strong>be prepared to dive in </strong>when you get a cryptic verifier error that drives you nuts.</p><p>Here’s the <strong>high-level </strong>architecture of MCPSpy, which is based on <strong>logical layers</strong>:</p><ul><li><strong>eBPF Programs &amp; Maps:</strong> The core monitoring programs.</li><li><strong>eBPF Loader (User Space):</strong> A Go program that loads the eBPF programs into the kernel.</li><li><strong>Event Handler:</strong> The component in user space that listens for events coming from the eBPF programs.</li><li><strong>MCP Parser:</strong> A parser that takes the raw data captured and interprets it according to the MCP protocol.</li><li><strong>Output Layer:</strong> Output the parsed information in useful formats.</li></ul><h4>MCP Monitoring Design</h4><p>Before writing the first eBPF code, we need to think about what we want to monitor.</p><p>MCP supports <strong>two main transport mechanisms, </strong>as mentioned <a href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports">in the spec</a>:</p><ul><li><strong>Stdio </strong>— The client and server communicate by reading from and writing to the <strong>standard streams </strong>(stdin/stdout) of the MCP server process. Therefore, monitoring MCP over stdio <strong>boils down to intercepting all read and write calls on those file descriptors.</strong></li><li><strong>Streamable HTTP</strong>—The client communicates <strong>requests </strong>to the server using <strong>HTTP</strong>, and the server <strong>streams </strong>back to the client using <strong>Server-Sent Events</strong> (SSE). We will explore this method in the next few chapters.</li></ul><p>In this first iteration of MCPSpy, I focused on the <strong>Stdio transport</strong>, since that’s the most common for local MCP servers today.</p><h4>The eBPF Program</h4><p>There are many different <strong>program types and hooks</strong> you can use in eBPF, but we won’t cover them all here — there are excellent resources like <a href="https://eunomia.dev/">eunomia.dev</a> if you want a broader overview. For MCPSpy, the critical question was: <em>where in the kernel should we hook to see MCP stdio traffic?</em></p><p>The answer lies in the Linux Virtual File System (VFS). All read() and write() system calls in the user space eventually flow through the internal kernel functions vfs_read and vfs_write. So, by attaching eBPF programs to vfs_read and vfs_write, we can observe every read and write operation happening in the file system.</p><p>Let’s walk through the flow of vfs_read as an example, with inline comments for explanation:</p><pre>// &quot;SEC&quot; is a macro to place this function in a specific ELF section <br>// so the eBPF loader knows it&#39;s an eBPF program for fexit.<br>SEC(&quot;fexit/vfs_read&quot;)<br><br>// BPF_PROG is an helper macro which expands the signature of vfs_read parameters,<br>// so we can use them directly within the program.<br>int BPF_PROG(exit_vfs_read, struct file *file, const char *buf, size_t count,<br>             loff_t *_pos, ssize_t ret) {<br><br>    // The reason we use fexit for this program is:<br>    // - fentry / fexit are more performant than kprobe / kretprobe<br>    // - we have access to entry parameters within fexit context.<br>    //   if we were using kprobe, we should hook both kprobe and kretprobe<br>    //   and transfer entry params to the exit hook which is less efficient.<br>    if (ret &lt;= 0) {<br>        // We logging only operations with data.<br>        return 0;<br>    }<br><br>    // Basic check if the data starts with &#39;{&#39;. (JSON-RPC protocol)<br>    // The idea is to filter the data as soon as possible,<br>    // because volume of data for read and write can be huge.<br>    if (!is_mcp_data(buf, ret)) {<br>        return 0;<br>    }<br><br>    // We are using ringbuffer to transfer the data to user-mode.<br>    // This is the preferred way to use ringbufs -<br>    // - First allocate the memory using bpf_ringbuf_reserve<br>    // - Then submit it through bpf_ringbuf_submit.<br>    struct data_event *event =<br>        bpf_ringbuf_reserve(&amp;events, sizeof(struct data_event), 0);<br>    if (!event) {<br>        bpf_printk(&quot;error: failed to reserve ring buffer for read event&quot;);<br>        return 0;<br>    }<br><br>    // We fill the data of the event. The data_event type<br>    // should be read in user mode in the same binary structure.<br>    event-&gt;header.event_type = EVENT_READ;<br>    event-&gt;header.pid = bpf_get_current_pid_tgid() &gt;&gt; 32;<br>    bpf_get_current_comm(&amp;event-&gt;header.comm, sizeof(event-&gt;header.comm));<br><br>    // Because we are limiting the actual copied size, <br>    // we preserve the original size as well.<br>    event-&gt;size = ret;<br><br>    // To satisfy the verifier, we need to bound the buffer to resonable size.<br>    event-&gt;buf_size = ret &lt; MAX_BUF_SIZE ? ret : MAX_BUF_SIZE;<br><br>    // Copying the r/w buffer to the event, according to the size.<br>    bpf_probe_read(event-&gt;buf, event-&gt;buf_size, buf);<br><br>    bpf_ringbuf_submit(event, 0);<br><br>    return 0;<br>}</pre><p><em>And that’s it! That’s the beauty of eBPF</em>.</p><h4>eBPF Loader</h4><p>To get MCPSpy bootstrapped quickly, I leaned on the <a href="https://github.com/cilium/ebpf/tree/main/cmd/bpf2go"><strong>bpf2go</strong></a> utility, which comes with the cilium/eBPF project. Combined with a //go:generate directive in loader.go, this made it easy to generate Go boilerplate that compiles the C eBPF code, creates bindings, and exposes the programs and maps directly as Go objects.</p><p>Of course, you still need to write the actual code to attach those programs to the right kernel hooks. In MCPSpy’s case, that meant hooking our program to vfs_read (and vfs_write) via a tracing attachment:</p><pre>// Attaching exit_vfs_read with Fexit<br>readEnterLink, err := link.AttachTracing(link.TracingOptions{<br>    Program:    l.objs.ExitVfsRead,<br>    AttachType: ebpf.AttachTraceFExit,<br>})<br>if err != nil {<br>    return fmt.Errorf(&quot;failed to attach %s tracepoint: %w&quot;, l.objs.ExitVfsRead.String(), err)<br>}<br>l.links = append(l.links, readEnterLink)</pre><h4>Event Handler</h4><p>The event handler is essentially a loop running in a <strong>separate goroutine </strong>that waits for events from the kernel and processes them. Here’s a simplified version of what the event handler loop looks like in MCPSpy:</p><pre>// The handler must run as a seperate go routine<br>go func() {<br>    ...<br>    // infinite loop, until the context is cancelled.<br>    for {<br>        select {<br>        case &lt;-ctx.Done():<br>            return<br>        default:<br>            // reading raw event (as bytes) from the ring buffer.<br>            record, err := l.reader.Read()<br>            if err != nil {<br>                ...<br>            }<br><br>            ...<br><br>            // We get the first byte to understand the event type.<br>            // Each event has different structure.<br>            eventType := mcpevents.EventType(record.RawSample[0])<br><br>            ...<br><br>            switch eventType {<br>            case mcpevents.EventTypeFSRead, mcpevents.EventTypeFSWrite:<br>                if len(record.RawSample) &lt; int(unsafe.Sizeof(mcpevents.FSDataEvent{})) {<br>                    logrus.Warn(&quot;Received incomplete data event for data event&quot;)<br>                    continue<br>                }<br><br>                // This is a highly effecient method to read the binary data<br>                // directly to the Go type.<br>                // It require the type to be in exact structure as the C type.<br>                if err := binary.Read(reader, binary.LittleEndian, &amp;dataEvent); err != nil {<br>                    logrus.WithError(err).Error(&quot;Failed to parse data event&quot;)<br>                    continue<br>                }<br>                event = &amp;dataEvent<br>            case ... &lt;other event types&gt;<br><br>            // Producing the events for further analysis.<br>            select {<br>            case l.eventCh &lt;- event:<br>            case &lt;-ctx.Done():<br>                return<br>            default:<br>                logrus.Warn(&quot;Event channel full, dropping event&quot;)<br>            }<br>        }<br>    }<br>}()</pre><h4>MCP Parser</h4><p>Finally, we get to interpret the content we’ve captured. Luckily, the protocol is relatively simple and has very convenient documentation here: <a href="https://modelcontextprotocol.io/docs/getting-started/intro">https://modelcontextprotocol.io/docs/getting-started/intro</a>.</p><p>Remember, for the stdio transport, the <strong>data we see in the read/write events </strong>is exactly the JSON text of <strong>MCP’s JSON-RPC messages</strong>.</p><p>MCP (being JSON-RPC 2.0) has a straightforward message format:</p><ul><li>There are <strong>requests</strong> with a method and id. Example:</li></ul><pre>{<br>  &quot;id&quot;: 6,<br>  &quot;jsonrpc&quot;: &quot;2.0&quot;,<br>  &quot;method&quot;: &quot;tools/list&quot;<br>}</pre><ul><li>There are <strong>responses</strong> with an id and either a result or error, but no method. Example:</li></ul><pre>{<br>  &quot;id&quot;: 6,<br>  &quot;jsonrpc&quot;: &quot;2.0&quot;,<br>  &quot;result&quot;: {<br>    &quot;tools&quot;: [<br>      { ... }<br>    ]<br>  }<br>}</pre><ul><li>And there are <strong>notifications</strong> which are like requests, but without an id because they don’t expect a response. Example</li></ul><pre>{<br>  &quot;jsonrpc&quot;: &quot;2.0&quot;,<br>  &quot;method&quot;: &quot;notifications/message&quot;,<br>  &quot;params&quot;: {<br>    &quot;data&quot;: &quot;Completed get_weather operation&quot;,<br>    &quot;level&quot;: &quot;info&quot;<br>  }<br>}</pre><p>MCP defines a specific set of <strong>method names </strong>on top of JSON-RPC. For example, some key methods include:</p><ul><li>initialize – used to initiate the connection (handshake between client and server, exchange capabilities, etc.).</li><li>tools/list – a request from the client to get the list of available tools from the server.</li><li>tools/call – a request to execute a specific tool on the server.</li><li>Similar patterns exist for other primitives, e.g. resources/get or prompts/list, etc., as defined in the MCP schema here <a href="https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.ts">https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.ts</a>.</li></ul><p>However, there’s a <strong>caveat</strong> due to how we capture the data: We see each message <strong>twice</strong> — once when it’s written by one side, and once when it’s read by the other side. For example, when the client sends a tools/call request, our eBPF hook will catch the <strong>write</strong> from the client process, and shortly after, the server process will <strong>read </strong>that data, which triggers our hook again.</p><p>To handle this, MCPSpy’s parser implements a simple <strong>correlation mechanism</strong>: when a write event is received, it stores the message (keyed by a hash of the content) and waits for the matching read event. Once the pair is seen, we continue processing it. This mechanism also helps us identify the process name and PIDs for both the client and the server.</p><h3>Output Layer</h3><p>After parsing the JSON and correlating pairs, the <strong>output layer</strong> takes over. At the moment, MCPSpy has two output modes: a <strong>human-friendly console </strong>printout and a <strong>machine-friendly JSON lines </strong>log.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3bEf6brA76FeL3YDK47jfg.gif" /></figure><h3>Summary</h3><p><strong>The ability to peek into MCP communications in real time is the first step</strong> toward stronger security and transparency for AI-driven tools. In the next part of this series, I’ll share the journey of extending MCPSpy to handle <strong>TLS-encrypted HTTP transports</strong>, which brings its own set of challenges (hint: we’ll likely involve user-space uprobe hooks for libraries like OpenSSL to decrypt on the fly). It promises to be an equally interesting adventure in eBPF and security. Stay tuned!</p><p><em>Hope you found this insightful and enjoyed the read. Feel free to reach out if you have questions or contribute to </em><a href="https://github.com/alex-ilgayev/mcpspy"><em>MCPSpy</em></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c445b76377cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>