Unlock the Power of Model Context Protocol with Serverless Architecture
The Model Context Protocol (MCP) is revolutionizing how AI applications connect to external data sources and tools. If you're looking to build your own MCP server and deploy it efficiently, Google Cloud Run offers a perfect serverless solution. Here's how you can go from zero to deployed in under 10 minutes.
What is MCP?
MCP is an open protocol that enables AI models like Claude to connect securely to external resources such as databases, APIs, and tools. Unlike traditional plugins, MCP provides a standardized way for models to discover and use capabilities without hard-coded integrations.
Prerequisites
Before we begin, ensure you have:
- A Google Cloud account
- Node.js 18+ installed locally
- Basic knowledge of JavaScript/TypeScript
- Google Cloud CLI installed and configured
Step 1: Initialize Your MCP Server Project
bash
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node
Create a tsconfig.json:
json
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
Step 2: Build a Simple Weather MCP Server
Create src/server.ts:
typescript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequest,
ListToolsRequest,
ToolSchema,
} from "@modelcontextprotocol/sdk/types.js";
const server = new Server(
{
name: "weather-mcp-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
// Define available tools
const tools: ToolSchema[] = [
{
name: "get_weather",
description: "Get current weather for a city",
inputSchema: {
type: "object",
properties: {
city: {
type: "string",
description: "City name",
},
},
required: ["city"],
},
},
];
// Handle tool listing
server.setRequestHandler(ListToolsRequest, async () => {
return { tools };
});
// Handle tool execution
server.setRequestHandler(CallToolRequest, async (request) => {
if (request.params.name === "get_weather") {
const city = (request.params.arguments as any).city;
// Simulate weather API call
const weatherData = {
temperature: Math.floor(Math.random() * 30) + 5,
condition: "sunny",
humidity: Math.floor(Math.random() * 50) + 30,
};
return {
content: [
{
type: "text",
text: `Weather in ${city}: ${weatherData.temperature}°C, ${weatherData.condition}, ${weatherData.humidity}% humidity`,
},
],
};
}
throw new Error("Tool not found");
});
// Start server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Weather MCP Server running on stdio");
}
main().catch(console.error);
Step 3: Create Deployment Configuration
Create Dockerfile:
dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/
ENTRYPOINT ["node", "dist/server.js"]
Create .dockerignore:
text
node_modules
npm-debug.log
src
tsconfig.json
Step 4: Build and Deploy to Cloud Run
Build your project:
bash
npm run build
Build and push container:
bash
gcloud auth configure-docker
gcloud builds submit --tag gcr.io/your-project-id/weather-mcp-server
Deploy to Cloud Run:
bash
gcloud run deploy weather-mcp-server \
--image gcr.io/your-project-id/weather-mcp-server \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--cpu 1 \
--memory 512Mi
Step 5: Test Your Deployment
Once deployed, you'll get a URL for your service. Test it with:
bash
curl -X POST https://your-service-url.a.run.com \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
}'
Integration with MCP Clients
Claude Desktop Configuration
Add to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json):
json
{
"mcpServers": {
"weather-server": {
"command": "curl",
"args": [
"-X", "POST",
"-H", "Content-Type: application/json",
"-d", "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{}}",
"https://your-service-url.a.run.com"
]
}
}
}
Best Practices for Production
Authentication: Add API keys or OAuth to secure your endpoint
Monitoring: Implement Cloud Monitoring and logging
Caching: Use Memorystore for frequently accessed data
Scaling: Configure min/max instances based on your needs
Error Handling: Implement comprehensive error handling and retries
Cost Optimization
Cloud Run automatically scales to zero when not in use, making it extremely cost-effective for MCP servers. You'll only pay for:
Compute time during requests
Memory allocated
Network egress
Example Repository
Check out our complete example on GitHub:
github.com/example/mcp-weather-server
Conclusion
Deploying MCP servers to Google Cloud Run provides a scalable, cost-effective solution for extending AI capabilities. With automatic scaling, built-in security, and pay-per-use pricing, you can focus on building powerful tools rather than managing infrastructure.
The entire process—from initial setup to production deployment—can realistically be completed in under 10 minutes, making it accessible for developers of all levels to contribute to the growing MCP ecosystem.
Top comments (0)