実行前に入力を検証する
不正な引数を明確なメッセージとともに拒否します。
「実行前に入力を検証する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Trust Nothing the Model Sends
The arguments your tool receives come from an AI, which can guess. Validate every input before you touch a file, a database, or an API.
Check First, Act Second
Run your checks at the very top of the function. Confirm the input is sane before doing any real or irreversible work.
@mcp.tool()
def set_age(age: int) -> str:
if age < 0 or age > 130:
raise ValueError("age out of range")Reject With a Clear Reason
When input fails a check, say exactly why. A precise message lets the model send a corrected value on the next attempt.
Type Hints Catch a Lot
Your parameter hints become the tool schema, so basic type mismatches are caught before your code even runs. Lean on them first.
@mcp.tool()
def repeat(text: str, times: int) -> str:
return text * timesHints Are Not Enough
Types alone cannot express ranges, formats, or allowed values. You still validate that a count is positive or a status is one you accept.
Bound Your Ranges
Guard numeric inputs against extremes. A limit on page size or loop count stops the model from accidentally overloading your server.
if limit > 100:
raise ValueError("limit must be 100 or less")Whitelist Allowed Values
For fixed choices, check membership instead of trusting free text. A whitelist rejects anything outside the options you actually support.
if status not in {"open", "closed"}:
raise ValueError("status must be open or closed")Sanitize Paths and Queries
Model input that becomes a file path or SQL query is dangerous. Sanitize it so a stray .. or quote cannot reach beyond what you intend.
Required Fields Must Exist
Confirm that anything mandatory is actually present and non-empty. An early guard beats a confusing failure deep inside your logic.
if not query.strip():
raise ValueError("query cannot be empty")Validation Doubles as Docs
Clear checks teach the model your rules. After one rejection it learns the format and sends valid input the rest of the session.
Fail Before Side Effects
The golden rule: never start an irreversible action until every input has passed. Validate up front so half-done writes never happen.
Quick Check
Why validate inputs before performing the tool's real work?
Recap
Treat model input as untrusted: validate types, ranges, and choices at the top, reject with clear reasons, and never act before checks pass. ✅
よくある質問
「実行前に入力を検証する」レッスンは無料ですか?
はい。「実行前に入力を検証する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「実行前に入力を検証する」で何を学びますか?
不正な引数を明確なメッセージとともに拒否します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「実行前に入力を検証する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。