AIエージェントに社内ツールを「安全に」渡す ― 30行から始めるMCPサーバー自作と境界設計入門A practical guide to building a minimal MCP server from scratch and designing…
社内ツールをAIエージェントへ安全に公開するため、最小限のコードでMCPサーバーを自作する手順と、権限境界設計の考え方を解説した実践的な入門記事。
A practical guide to building a minimal MCP server from scratch and designing safe permission boundaries so AI agents can access internal tools without security risks.
AI 生成の要約と解説(解説は要約と収集メタデータに基づく)で、元記事全文の転載・翻訳ではありません。日本語版と英語版は言語ごとに独立して生成しています。正確性は元記事 (zenn.dev) でご確認ください。AI-generated summary and explainer (the latter based on the summaries and collected metadata), not a copy or translation of the full source article. Generated independently for each language. Verify accuracy at the original source (zenn.dev).
AIエージェントに社内のデータベースやAPIといったツールを使わせたい――そうした需要が高まる中、両者をつなぐ標準として注目を集めているのがMCP(Model Context Protocol)だ。本記事は、最小限のコードでMCPサーバーを自作しながら、権限の境界をどう設計すべきかを実践的に解説する入門ガイドである。
MCPは、Anthropicが2024年に公開したオープンな仕様で、AIモデルと外部のツールやデータソースを標準化された方法で接続することを目的とする。従来は各AIクライアントやエージェントごとに独自の連携を作り込む必要があったが、MCPを介せば「サーバー」としてツールを一度公開するだけで、対応する複数のクライアントから再利用できる。ClaudeやChatGPT、各種エージェント開発フレームワークが対応を進めており、事実上の共通インターフェースになりつつある。
記事が強調するのは、MCPサーバー自体は決して複雑なものではないという点だ。ツールの名前、入力スキーマ、そして実際の処理を記述すれば、30行程度のコードでも動く最小構成が作れる。まず小さく作って動かし、そこから機能を足していくアプローチが、仕組みの理解には有効だと見られる。
ただし、社内ツールをエージェントに渡すうえで本当に難しいのは、コードの量ではなく「どこまでの操作を許すか」という境界の設計だ。エージェントは与えられたツールを自律的に呼び出すため、読み取り専用にとどめるのか、書き込みや削除まで認めるのか、対象データの範囲をどう絞るのかを、あらかじめサーバー側で明確に制御しておく必要がある。認証情報の扱いや、想定外の入力に対する検証も欠かせない。
プロンプトインジェクションなどを通じて、エージェントが意図しない操作へ誘導されるリスクも指摘されており、最小権限の原則に沿って公開範囲を絞ることが現実的な防御策となる。まず手を動かして動く最小のサーバーを作り、そのうえで境界設計を考えるという記事の進め方は、MCPを社内へ安全に取り入れたい開発者にとって手がかりになりそうだ。
Model Context Protocol (MCP) has rapidly become one of the most discussed ways to connect AI agents to the systems that make them genuinely useful, and this tutorial addresses a problem that surfaces the moment teams move past demos: how to expose internal tools to an agent without granting unchecked access to sensitive systems. Its central argument is that a working MCP server can be assembled in roughly thirty lines of code, but that the more consequential work lies in designing the permission boundaries around it.
MCP is an open protocol, originally published by Anthropic in late 2024, that standardizes how AI applications communicate with external tools and data. Instead of writing a bespoke integration for every model and every service, developers run an MCP server that advertises a set of capabilities, and any compatible client—an AI agent, an IDE assistant, or a chat interface—can discover and call them. The protocol is built on JSON-RPC and typically exposes three kinds of primitives: tools, which are actions the agent can invoke; resources, which are data it can read; and prompts, which are reusable templates. Because the interface is uniform, one server can be reused across different agents and hosts.
The "thirty lines" framing reflects how much the tooling has matured. Official SDKs in Python and TypeScript, along with helper frameworks such as FastMCP, reduce a basic server to a handful of function definitions decorated to mark them as callable tools. Transport is usually handled over standard input/output for local processes or over HTTP for networked deployments, so a minimal server can register a function, describe its inputs, and run without much boilerplate. This low barrier is precisely why the article stresses that the code is the easy part.
The security discussion is where the guide appears to concentrate its value. Once an agent can call a tool, it can also be manipulated into calling that tool in unintended ways, whether through flawed reasoning or through prompt injection embedded in the data it processes. The recommended approach follows the principle of least privilege: each tool should do the smallest useful thing, expose only the parameters it truly needs, and validate every input rather than trusting the model to behave. Separating read-only operations from write or destructive ones, and gating the latter behind explicit human confirmation, is a common pattern for limiting blast radius.
Boundary design also extends beyond the individual function. A well-scoped server authenticates the caller, enforces authorization per tool, and logs each invocation so that actions can be audited after the fact. Sensitive credentials should live on the server side rather than being passed through the model, and outputs returned to the agent should avoid leaking more of the underlying system than necessary. Treating the agent as an untrusted client—much as a web backend treats a browser—is a useful mental model, because the language model sits at an unpredictable trust boundary between user intent and system capability.
This attention to safety mirrors broader industry movement. After Anthropic introduced MCP, support was signaled by other major vendors, including OpenAI and Google, which helped position it as a de facto interoperability layer for agent tooling rather than a single company's format. That momentum makes the security questions more urgent, because a shared protocol means the same misconfigured server could be reachable by many different clients. Security researchers have already highlighted risks such as tool poisoning, over-broad scopes, and confused-deputy scenarios, so guidance on careful boundary design is timely.
For readers, the useful takeaway is a mental separation between capability and permission. MCP solves capability: it gives agents a clean, standardized way to reach tools. Permission remains the developer's responsibility, and it is where most real-world risk concentrates. Adjacent concepts worth understanding before building include JSON-RPC basics, the distinction between local and remote transports, and existing authorization patterns like OAuth, which later versions of the specification have moved to incorporate for HTTP-based servers.
The article is best read as an entry point rather than a complete security framework. Building a minimal server demonstrates the mechanics, but production deployments in an organization would likely require threat modeling, rate limiting, and monitoring beyond what a short tutorial can cover. Its lasting contribution is reframing the task: the goal is not merely to make internal tools reachable, but to decide, deliberately, exactly how much of them an agent should ever be able to touch.
ポコとTECHガイドPoko & TECH Guide
ポコと当サイトのTECHガイドが、記事の要約と収集した情報から読みどころを語るAI生成の対話です。Poko and TECH Dashboard's own TECH Guide discuss the article's highlights in an AI-generated conversation based on its summary and collected source details.
- ポコPoko社内のDBやAPIをAIに触らせたいとき、この記事の方法だと普段の何が変わるの?If I wanted an AI to use our internal DB or APIs, what actually changes day to day with this approach?
- TECHガイドTECH Guide関数やAPIキーをそのまま渡す代わりに、MCPサーバーを自分で作って、その窓口からだけツールを公開します。記事では、それで「こわさ」の多くを減らせると書かれています。Instead of handing over raw functions or API keys, you build your own MCP server and expose tools only through it. The article says this can ease much of that worry.
- ポコPoko関数をそのまま渡すやり方は、前はダメだったの?So was passing functions directly not possible before?
- TECHガイドTECH Guideできるし、小さな検証なら十分だそうです。ただ実務だと、接続が N×M 本になる、権限やログを入れる場所が無い、使い回せない、という3つの壁が出ると書かれています。It works, and the article says it's fine for small tests. In real work, though, it lists three walls: N×M connections, no place for permissions or logs, and no reuse.
- ポコPokoじゃあ一番の収穫は、サーバーを1つ書けばどのホストでも同じツールが使えて、境界も作れるってことかな?So the big takeaway is: write one server, use the same tools from any host, and you get a boundary too?
- TECHガイドTECH Guideその読みで合っていますよ。N×M が N+M になるのが大きな利点、と記事にもあります。公開できる3種類のプリミティブの使い分けが設計の肝、という点も注目です。That's right. The article calls N×M becoming N+M the major design benefit. It also says choosing among the three primitives is the heart of the design, so watch that part.
匿名の公開いいねです。記事の保存・お気に入りではなく、Featured、Top 3、重要度、掲載順位には影響しません。仕組みとプライバシーAnonymous public likes are reactions, not saved articles or bookmarks. They do not affect Featured, Top 3, importance, or listing order.How it works and privacy



