Build a Starknet MCP Server in 15 Minutes - Connect Claude AI to Blockchain

Nethermind · 23 Apr 2025 · 18:38

A hands-on walkthrough of building a Model Context Protocol server that lets Claude read live Starknet data. The presenter introduces MCP as a USB port for AI agents, defines servers, resources, tools and prompts, then codes a single-file server with a tool that checks token balances for a wallet address. The server is registered in Claude Desktop and tested against a real address, with the assistant answering from the live blockchain rather than its training data. A practical starting point for developers curious about connecting AI agents to onchain data.

Topics

From Nethermind; community: Nethermind.

Transcript

Today, most of the powerful AI assistants like Claude have one massive blind spot. They can't see data outside of the context to which they are trained on. But today, I'm going to show you how to fix that in just 15 minutes using MCPS and with one simple file. Now, most tutorials would have you build complex multi systems that take hours to set up. But it turns out the real magic happens when you strip away everything that is unnecessary and focus on the core functionalities between these two technologies that is AI agents and blockchain. So by the end of this video you'll have your cloud desktop checking starket wallet balances in real time and I guarantee you it will blow your mind

on how simple this actually is. So let's just dive in. Before we dive into the code, I want to explain why this project is actually so important. Think about it. We have these incredibly smart AI assistants like Claude that can reason, write code, analyze data, but they have a major limitation. They are essentially trapped in their training data with no ability to access realtime information from the outside world. So, here's the problem that we're trying to solve today. When you ask Claude, "What's the balance of my Starknet wallet?" It simply can't tell you. Not because it does not understand the question, but because it has no way to reach out and check the blockchain data in real time. It's like having a

brilliant adviser who is stuck in a room with no windows or doors. They can give you amazing insights and advices based on what they already know, but they can't look outside to see what's happening in real time. On the other side, we have Starknet, which is this powerful L2 with all kinds of valuable data like account balances, transaction histories, smart contract states, and all sorts of things updating in real time. But this data might as well be invisible to the AI assistants that we use today. And this is where MCP, the model context protocol, comes into picture.

And I and I like to think of MCP as installing a USB port to the AI agent that we want to use in our daily life. So here's the official definition of what an MCP is. So the model context protocol lets you build servers that expose data and functionality to LLM applications in a secure and standardized way. Think of it like a web API, but specifically designed for LLM interactions. So let me break this down for you into more simpler terms. MCP is basically a standardized way for AI models to access external data and functionality. Where traditional APIs connect web services together, MCP is specifically designed

to connect AI models to the outside world. So an analogy that we can take is that before the invention of USB connecting devices to different computers was a nightmare because every computer had a different port, different cable, different driver. So USB standardized all of that. Similarly, MCP gives us a standard way for AI to interact with external tools and data at the same time. So according to MCP's documentation, there are four key words that you should understand as we'll be using them in the code today that we do. So first is servers. So these are foundations of

MCPS. A server handles connections, manages communication and routes messages between the AI and the code that we write. In our project, we'll create a server that connects to stacknet blockchain. Next, we have resources. So these provide data to the AI similar to the get endpoints in the rest APIs. Resources load information into the AI's context without performing major computation. Then we have tools. So these enable AI to take actions which is similar to a post endpoint. So tools execute code or interact with external systems. Today we'll build a tool that checks token balances on stacknet. And

last but not the least, we have prompts. So these are reusable templates that help guide AI interactions with your server that you code. So for our Starken project, we'll focus primarily on creating a server with tools. So let me break down how this actually works in the background. First, we have MCP servers. These are like specialized adapters that connect to specific data sources or tools. That's what we are building today. A server that connects to Starnet. Then we have MCP clients built into AI applications.

So these are like USB ports in your computer. So Claude has an MCP client builtin which lets it discover and connect servers like the one we are making today. Finally, we have MCP hosts like Claude desktop. These manage connections and handle permissions making sure everything works securely. So here's what happens when you ask Claude about your Starnet wallet in the project that we are making today. You ask Claude about a wallet balance. Claude recognizes that it needs blockchain data that it doesn't have right now. It sees that it has a MCP server which is available through its client. Claude then sends a request to our server with the wallet address. Our

server tool which we created then queries the Starknet blockchain. The data flows back through the same channel to Claude again and Claude uses the real-time data to answer the question that we asked it in the first place. So what's powerful about this approach is that we are not just building one of the solutions. The MCP standard means our server can now work with any AI assistant that supports MCP and it's not just limited to cloud and we are not just limited to checking balances. Once we understand the pattern, we could extend this to view transaction histories, monitor smart contracts or even initiate transactions

but with proper security measures of course. But the real magic is how this transforms the user experience. Instead of jumping between tools like checking a blockchain explorer then coming back to claude, you get a seamless experience where the AI can directly access the data it needs to help you. Now that we understand what we are building today and why it matters, let's get our hands dirty and start coding this bridge between AI agents and stagnet.

Okay, now that we understand what MCP is, let's build our project. Uh so instead of typing every command, I'll be copy pasting the commands and explain you as we use them. So this keeps our video focused and concise. So the first command that I'm putting is to create a folder and moving into it. The next command would be to initialize an npm project with a - y flag. So the - y flag accepts all the default so we don't have to answer a bunch of questions. And now for the dependencies. Here's where most tutorials get complicated, but they'll have you install a dozens of packages, but we only need two essential packages.

So the first line installs the M mcp server SDK and the starknet library. So starknet library means the starket js library and the second line installs typescript and nodejs type definitions for development. Now after this has been done we need to have a typescript configuration file. Uh this is critical because stacknet uses big int literal. So this is the typescript uh tsconfig file. So let me create this from the CLI and then open it up and explain you why we did what we did. So this is the TS config file. So we are basically targeting ES2020 because it supports big int

literal which uses extensively and we have one more step which is to change something in the package.json. So basically we would have to add a type which would be for module and then we would have to add a script for build process build ts. So these two additions tell NodeJS that we are using ES modules and a build typescript that runs TypeScript compiler when we run this command. And just like that, our foundation is set. Uh four simple commands and we are ready to start

building this project. And uh now comes the exciting part. We are going to create a single file that will be the place for our MCB server and the interactions that we do with starknet. So basically we would have to create one file name it as uh index.ts. Okay. And I'll break this down from here onwards into logical sections and explain each one as we go. So first we need to import the necessary libraries. The MCP SDK gives us the server class. The stdio server transport uh which handles the communication

between claude and our code. We also import the schema definition that defines the structure of our requests and responses from starket library. the starket js library. We need the contract class to interact with the token contracts and the RPC provider to connect to the network. The provider here is created to connect to the starknet mainet using a public RPC endpoint. This gives us a way to read blockchain data without running our own node. Next, we need to define token contract addresses for ETH and stark.

You can add as many tokens as you want. For demonstration purposes, I have added only two. The main tokens, these are the main net token addresses on starket. So starket addresses are 64 hexa decimal character after the 0x prefix which is longer than ethereum's 40 characters. So just keep this point in mind because we'll be creating some specific functions to check whether a given address is stark address or not. We also define a minimal AI here which is essentially a description of the functions available on these token contracts. Since we only need to check balances, we are including just the balance of uh function. This function takes an account address and returns the

token balances for that address. Now we need to define two helper functions format balance and is valid stacknet address. So the format balance function converts the raw number format from the blockchain into something readable to human. So token amounts on the blockchain are stored as integers without decimal points. So for ETH and stark one whole token is represented as one followed by 18 zeros. So this function inserts the decimal point in the right place. That's it.

It's very simple. The next is is valid starknet address which is basically a function for simple validation check that ensures addresses match the expected stark net format. This helps prevent errors when we are quering the blockchain. So the next step is to have the main function where the actual blockchain interaction happens. It takes a wallet address and a token symbol as inputs. then looks up the contract address using the AI we defined earlier, the tokens address and our provider that we created initially. This gives us a JavaScript interface to the token contract. Then we call the balance of

function on the instance uh passing the wallet address. This is an asynchronous call that fetches data directly from the blockchain. And finally we format the data which we got using the helper function which we created earlier and we return these values from this function. Now we create the MCP server itself. Okay. So first we initialize it with a name and a version. So here I'm writing it as starknet reader and the version is 1.0.0. So and specify it will provide tools. So this is what I'm saying that these are the capabilities and these are the tools that it's going to provide and this is the definition of the available

tool telling claude that what what we can do. So we implement a handler for list tools request which is called when an AI like Claude wants to know what capabilities our server has. We define a single tool called check balance here that accepts two parameters an address and a token symbol. We also mark both parameters as required to ensure Claude provides all necessary information. This is essentially the menu of capabilities that Claude will see when it connects to our server.

Next, we implement the handler that executes when Claude calls our tool. So this handler receives the request from claude, processes it and then returns the result. First we extract the tool that we created called check balances since that's the only tool we support right now. So we then validate the inputs which is here. Making sure the addresses and token parameters are provided, the address follows the correct stark net format and token is either ETH or stark for now. So if everything checks out, we call our check balance function here with the provided parameters and

return the result as a formatted JSON string. We also implement an error handling to provide clear messages if anything goes wrong during the process. This helps claude understand what happened and potentially retry with corrected parameters. Finally, we start with the main server. So, we create a stddio server transport here which handles communication between claude and claude desktop and our server using the standard input output streams. So, we connect our server to this transport and log a message indicating that the server is running. Note that we use console.

Because the standard output is reserved for the MCP protocol communication. We also add an error handling to catch any unexpected error that might occur during server operations. If a fatal error happens, we log it and exit the process. And that's it. With just about 100 lines of code, we have created a complete bridge between Claude and the Starnet blockchain. This entire implementation is very clean, focused and handles all the necessary communication between Claude and Starknet. Now let's compile and connect our server to Claude. So first I need to open up the terminal and uh let me clear this terminal and put the build command.

npm run build. So this compiles a TypeScript into JavaScript in the dist folder that you can see just came up here in the uh project directory. So now comes the moment where we connect our server to claude. This happens inside claude desktop or you can directly configure the uh claude config uh file uh on your desktop. So I have that file here. You can find it in the claude uh folder wherever it is on your PC. So I'm just opening this cloud desktop config JSON file and and I need to add our uh server configurations here. So I already have a server here. So let me

just uh change the name because we created this folder as darknet mcp. So let me do that. And here we go. So we need to make sure that the full path to the index.js gs file in the disk repository should be placed uh which is the compiled javascript file and then we need to save this configuration and toggle the switch to the starknet reader in the cloud desktop app. So let me open cloud for you now. So as you can see we have one MCP tool which is available and it's called check balances which we just created.

Now for the coolest part. Let's see this thing in action. So let me ask Claude about a wallet address. So can you check the assets of the address? Uh we can paste this address here and let claude go to action. Okay. So we need to allow this for the chat. And so incredible Claude just pulled realtime data from the Starket blockchain and this is accessing data that's not a part of Claude's training data. It's querying the live blockchain

right now. So just like that we have given Claude the ability to see the blockchain data in real time. So this is just a start. Think about the possibilities. This opens up AI agents that can monitor your portfolio, alert you when significant transactions happen on blockchain or maybe help you make more informed decisions based on some onchain data. So this intersection of AI and blockchain is where some of the most exciting innovation is happening right now and you are now at the forefront of it.

So, let your imaginations roll out to something amazing. And thanks for building with me today. And I'll see you in the next video.

Related talks