Building My First MCP Server: A DNS Query Tool
Building My First MCP Server: A DNS Query Tool
In this post, I'll share my experience building a Model Context Protocol (MCP) server that provides DNS querying capabilities. This project demonstrates how to create a standardized interface for DNS operations using TypeScript and Node.js.
What is MCP?
Model Context Protocol (MCP) is a standardized way to expose functionality to AI models. It provides a consistent interface for tools and services to interact with AI systems, making it easier to build and maintain AI-powered applications. MCP is a open protocol initially developed by Anthropic, which was announced in Nov, 2024. Since then, MCP has became yet another buzz word in the AI industry, and people has implemented many different servers and clients of MCP.
Project Overview
The MCP DNS project is my first MCP server. With this simple implementation, I got some end-to-end experience about implementing MCP server and using it in AI tools, like Claude Desktop App. This MCP server supports making DNS query for a given name and type, and it will not difficult to add more features to it.
Server Implementation
This MCP server was implemented following the official quickstart guideline
- Initialize the project
# Initialize a new npm project
npm init -y
# Install dependencies
npm install @modelcontextprotocol/sdk zod
npm install -D @types/node typescript
# Create our files
mkdir src
touch src/server.ts
- Create server instance
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "dns",
version: "1.0.0",
capabilities: {
resources: {},
tools: {},
},
});
- Implement tool
We will implement the first tool on the MCP server, which is responsible for making DNS queries. For implementing it, we will need to define the name, description, the JSON schema for the tool's parameters, and the actual handler.
...
import dns from "dns/promises";
...
// Register the tool with the server
server.tool(
// name
"dns-query",
// description
"Make a DNS query for a given name and type",
// parameters schema
{
name: z.string().describe("The domain name to query"),
type: z
.string()
.describe("The DNS record type (A, AAAA, MX, TXT, CNAME, NS, etc.)"),
},
// handler
async ({ name, type }) => {
try {
if (!name || !type) {
// Input validation
}
const resolver = new dns.Resolver();
resolver.setTimeout(5000);
const records = await resolver.resolve(name, type);
return {
content: [
{
type: "text",
text: JSON.stringify({
domain: name,
type: type,
records: records,
}),
},
],
};
} catch (error) {
// Handle DNS resolution errors
return {
isError: true,
content: [
{
type: "text",
text: `Error resolving DNS: ${error.message}`,
},
],
};
}
}
);
- Run the server
After implementing the tool, we will implement the main function to run the server:
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("DNS MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});
Use MCP server in Claude Desktop App
After implementing our MCP DNS server, we can use it with Claude Desktop App to perform DNS queries through natural language. Here's how to set it up and use it:
- Build and Run the Server
First, we need to build our TypeScript server:
# Build the TypeScript code
npm run build
- Configure Claude Desktop app
In Claude Desktop App, we will configure MCP servers for Claude Desktop app by editing the config file ~/Library/Application Support/Claude/claude_desktop_config.json
. The following should be added to the config file:
{
"mcpServers": {
...(other MCP servers)...
"dns-query": {
"command": "node",
"args": [
"/{{FULL-PATH-TO-THE-PROJECT}}/build/server.js"
]
}
}
}
- Using the DNS Query Tool
Once configured, you can use the DNS query tool through natural language in Claude Desktop App. For example, the following screenshot is my testing conversation:
In this conversation, I asked:
What is the DNS address of google.com?
Claude automatically did the following:
- Recognize the DNS query intent
- Format the appropriate parameters
- Call the MCP server
- Present the results in a readable format
And then, Claude gave me the answer, with some additional explaination about the potential limitation of this result.
The DNS A record for google.com is:
IP address: 172.217.14.206
Lessons Learned
Building this MCP DNS server provided several valuable insights:
-
MCP Protocol Design
- The protocol is well-designed for tool integration, making it straightforward to expose functionality to AI models
- The tool definition structure is clear and flexible, allowing for both simple and complex implementations
-
Development Experience
- The MCP SDK documentation is comprehensive and easy to follow
- Testing tools with AI models requires a different approach than traditional unit testing
- Local development and debugging of MCP servers is straightforward
-
AI Model Interaction
- With MCP servers, LLM models, like Claude, will not only understand the user intent in the conversation, but also have the ability to retrieve additional information or carry on certain actions that can change the states.
Next Steps
There are several ways to enhance and extend this project:
-
Feature Enhancements
- Implement DNS server selection for queries.
- Implement more complex logic like diagnosing a broken DNS setup.
-
Technical Improvements
- Add comprehensive error handling.
- Implement security mechanisms to prevent abuse.
-
User Experience
- Improve error messages to be more user-friendly
- Add support for DNS query history
Conclusion
Building an MCP server for DNS queries was a great learning experience. The project shows that even simple tools can be powerful when properly integrated into the AI ecosystem.
You can find the complete implementation on GitHub.