Early Bird Discount Available:
Live Cursor Workshop with John Lindquist
Claim your Spot

Generate and Debug MCP Servers from Scratch

Scraped MCP Examples ghx tool

Building complex components like Model Context Protocol (MCP) servers from scratch can be challenging, especially without good reference implementations. This lesson demonstrates a powerful workflow combining the ghx CLI tool with AI assistants (like Google's AI Studio and Cursor's agent) to overcome this hurdle.

You'll learn how to:

  1. Use ghx: Leverage the ghx command-line tool (which wraps the GitHub CLI) to search GitHub for real-world code examples based on specific import paths (e.g., modelcontextprotocol/sdk/server/mcp.js).
  2. Refine Search Results: Use ghx options (--help, --limit, --max-filename, NOT demo) within Cursor's chat to filter results, exclude simple demos, and gather a rich set of relevant MCP server implementations.
  3. Provide AI Context: Copy the aggregated code examples found by ghx into an AI environment like Google AI Studio (or ChatGPT).
  4. Handle Context Limits: See techniques for trimming large context files (like using Cursor's Cmd+K inline edit with head -n commands) to fit within model limits while retaining valuable examples.
  5. Generate Code: Prompt the AI (Gemini 2.5 Pro in AI Studio or ChatGPT O3) to generate a new MCP server (e.g., with five distinct tools) based solely on the provided real-world examples.
  6. Integrate and Debug: Set up the generated TypeScript code (mcp_server.ts) locally in Cursor, install dependencies (pnpm install), and use Cursor's agent (Cmd+I) or inline fixes (Cmd+K) to resolve TypeScript errors and path issues.
  7. Configure in Cursor: Add the newly created MCP server (running via bun mcp_server.ts) to Cursor's MCP settings, using the absolute path and the bun command.
  8. Test the MCP Server: Interact with Cursor's chat agent to call the custom tools (e.g., add_note, list_notes, write_managed_file) defined in the AI-generated MCP server, verifying its functionality.

This lesson showcases how combining targeted code searching (ghx) with the generative power of AI, fueled by relevant examples, can dramatically accelerate the process of building and bootstrapping complex tools and applications.

Share with a coworker

Transcript

[00:00] I built a tool called ghx. It wraps around the GitHub CLI and it makes scraping examples from GitHub much easier. And if you run ghx help from the terminal you'll see it shows you all of the options but it also shows you a bunch of examples at the bottom and if you're building CLI tools I strongly suggest adding examples when people run help because it allows you to do stuff like selecting all this saying add to chat all of those examples are in context and I can say please use the GHX tool to find TypeScript files with the following terms. And we'll go ahead and paste in the import from the MCP server SDK, which just looks like this in my other editor, which makes imports a great way to search for examples you're looking for. And then I'm also going to exclude the word demo so that I don't just get hello world examples.

[00:54] So I'll say please exclude the word demo from the search. And then I'm also going to add in Please increase the limit to 100 examples and increase the limit of each file to like a thousand lines. So if we run this it'll be able to determine based on the examples what command to build up and run. So now that it built the command it's running the command, now it's searching through GitHub, finding all the results, and it will concatenate them all together into a single file for us. So the results are ready.

[01:25] If you click on this it opens in the editor. I'll hit command-i to close the agent for now, ctrl-backtick to close the terminal, And this file is just saved in a standard library preferences path based on your machine. And this is going to be a fairly large file, it's about 17, 000 lines long, full of MCP server examples. If you want to navigate it a little bit better you can jump into the outline view. And the header threes are organized by the repo that this was pulled from so you can kind of jump around them and get an idea of what you're looking at.

[01:58] But for now I'm going to copy and paste this entire thing. I'm going to jump over into AI.dev and paste all of the examples. Let me fullscreen this and zoom in a bit, command down to jump to the bottom. And since we're full of examples here we can essentially generate anything we want at this point. We can say based on these examples please create an MCP server with five tools that have various functionality to show off what MCP is capable of and for now let's just avoid anything with authentication.

[02:30] Then let that paste in, command enter to run, And I'm also going to grab this, copy markdown, and hop over to chat-gpt. I'm going to test it on 03 as well, which is recently released. Exact same query. Kind of compare the results. This one is too long.

[02:45] Let's see what the token count is. So 144, 000 tokens. So I think in our editor I'll go back here, I'll hop into the terminal, and I'll press command K. I'm going to reference this file that we have and say please copy the first 12, 000 lines of this file to the clipboard. I'll hit submit.

[03:06] So it generates a command. I'll hit command-enter to run this. So now when you go back to chat-gpt we can edit our prompt, paste everything in, and send a similar query like, based on these examples, please generate an example MCP server that has five tools that do various things. Let that paste in and this should have brought us, unfortunately that's still not in the context window. Let me crop it down even more.

[03:33] Let's try 8, 000 lines. Swap, edit, select all, paste. And I actually have a history in Super Whisper where I still need to build a tool that lets me paste things from history. So I'll come up and copy this rather than just saying it again. Click send and now it looks like we're within the limit.

[03:53] If there is less context in some of these models, scraping the more high-quality examples rather than just completely in bulk is going to be important. And let's check on AI Studio. I'm going to zoom out a tiny bit. So it looks like it ended with some instructions on how to run it. So let's capture this file.

[04:10] We just need to copy this, swap back, close that down, Command Z to clear the terminal. And then since it asked us to name this file MCPServer, we'll just go ahead and type cursor MCPServer.ts and this will create the file in the root of our project. I could have done it through Explorer but sometimes if you're in the terminal just do things in the terminal. So I'll paste our script here, hop back and follow the rest of the setup. I'm gonna use npm so I'll paste this in, I'll change it over to pnpm, it's my preference.

[04:42] Swap back and it looks like it's requesting a manage files directory so that's fine we're probably gonna have to reference those. And since we still have some red in here, if you hover over an error or if you go down into problems, so if we find all these errors we can select command all, we'll just copy here, command I, then command new for a new chat, hit paste, and then fix please. It does look like something from my search results. I do have some bold highlighting when using the GHX tool. I should probably turn that off for this scenario, but it determined that and figured it out.

[05:18] And I can just accept these changes. So let's grab these two errors, command-A, command-C, command-P, please fix. These do have an option of right-clicking fix with AI, but that does the fixes inline, whereas your agent will always have more context. So I'll hit enter here. Then we should be done.

[05:37] There's no more problems reported. We can swap over to the terminal. Then I assume our instructions will be to run this next. When running a TypeScript file by itself I usually just use bun. So I'll say bun mcp server.

[05:49] It allows me to skip the compilation step. I'll hit enter. This server should be up and running. And since the code that was generated was a standard I O transport To add this we're going to open our cursor settings, go into MCP, we'll add a new global MCP server, we'll call this examples, and the command is going to be bun, and the args are going to be path to this file. We can right-click, copy path, and change the args here to that path, and remove the trailing comma.

[06:24] Then once we look back at our cursor settings we'll have this examples MCP server ready to rock. I'm going to create a new chat with no context. I'm going to say please add a note that I need to go shopping for dinner on Sunday. Then hit enter and it's going to call our MCP tool for adding notes. And if I check in my manage files I don't see it in there.

[06:45] So we may need to figure out ourselves where exactly this is creating this directory. So this path will be relative to where bun was invoked. So we can actually check on where that was by going into output and going into the cursor MCP logs. We'll drag this up a bit. We'll scroll back up to see where it talked about managed files and it looks like it is trying to create that in the root, which is obviously not going to work out for us.

[07:15] So let's just hard code that to this path for now. We'll copy path, then paste this path in. We'll hit save here, and then scroll down to the logs so we can watch the cursor MCP logs. Then go into our cursor settings. We're going to clear this output first so we can make sure we only have the new output.

[07:32] We'll hit refresh on examples and then make sure at the top that we have successfully created our managed files directory. This is showing an error, let me check on it. Oh that's just a console error, that could have been a console log, That's just informational. So if we try and run this again I'll come up here, just let it run again, and we'll check our managed files and we still don't see a file. Let's see what happened down in the logs.

[07:58] So let's see what add note actually does. It looks like it's adding a notes array and this is in memory. I command click to do that and then we'll look for references if this ever gets written to a file. So I did have the assumption that it would save to managed files and that was totally wrong. It's just saving it in memory.

[08:19] So if we say please list all of my notes, hit enter, it should be in memory at least. And I'm just curious what managed files was doing this whole time. So we make the directory, oh there's a read managed file tool, and that is it. It looks like it only uses this write manage file and read manage files. So we can try saying please write all of my notes to my managed files.

[08:46] Then it will call the MCP tool to list the notes, and then it will go and write that to a managed file. So now if we check on managed files, now we have a notes.txt and everything is working together.