{"id":96779,"date":"2026-08-28T11:16:48","date_gmt":"2026-08-28T03:16:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/96779.html"},"modified":"2026-08-28T11:16:48","modified_gmt":"2026-08-28T03:16:48","slug":"%e7%ac%ac%e5%9b%9b%e7%ab%a0-fastmcp-%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%a1%86%e6%9e%b6%e4%b9%8b%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e9%92%a9%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/96779.html","title":{"rendered":"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50"},"content":{"rendered":"<h3>\u76ee\u5f55<\/h3>\n<li>\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0<\/li>\n<li>lifespan\u53c2\u6570\u914d\u7f6e<\/li>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u793a\u4f8b\u5206\u6790<\/li>\n<li>lifespan_wrapper\u673a\u5236\u89e3\u6790<\/li>\n<li>lifespan_context\u8bbf\u95ee\u65b9\u5f0f<\/li>\n<li>\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u6a21\u677f<\/li>\n<li>\u5e94\u7528\u573a\u666f<\/li>\n<h3>\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0<\/h3>\n<p>FastMCP\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u670d\u52a1\u5668\u542f\u52a8\u548c\u5173\u95ed\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u8d44\u6e90\u7ba1\u7406\u64cd\u4f5c\u3002\u8be5\u673a\u5236\u901a\u8fc7lifespan\u53c2\u6570\u914d\u7f6e\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u5b9e\u73b0\u670d\u52a1\u5668\u751f\u547d\u5468\u671f\u7684\u7cbe\u7ec6\u5316\u63a7\u5236\u3002\u751f\u547d\u5468\u671f\u94a9\u5b50\u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u9700\u8981\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u521d\u59cb\u5316\u3001\u5728\u5173\u95ed\u65f6\u6e05\u7406\u7684\u8d44\u6e90&#xff0c;\u5982\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u3001\u7f13\u5b58\u5ba2\u6237\u7aef\u548c\u5916\u90e8\u670d\u52a1\u8fde\u63a5\u7b49\u3002<\/p>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>server.py<\/li>\n<\/ul>\n<h3>lifespan\u53c2\u6570\u914d\u7f6e<\/h3>\n<p>lifespan\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u8be5\u5bf9\u8c61\u8fd4\u56de\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002\u5728Server\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d&#xff0c;lifespan\u53c2\u6570\u88ab\u76f4\u63a5\u8d4b\u503c\u7ed9\u5b9e\u4f8b\u5c5e\u6027&#xff0c;\u7528\u4e8e\u7ba1\u7406\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u3002\u5bf9\u4e8eFastMCP\u7c7b&#xff0c;lifespan\u53c2\u6570\u901a\u8fc7lifespan_wrapper\u51fd\u6570\u8fdb\u884c\u5305\u88c5&#xff0c;\u4ee5\u9002\u914d\u5e95\u5c42\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u673a\u5236\u3002<\/p>\n<p>  #mermaid-svg-pamXGzapMzRYZthl {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-pamXGzapMzRYZthl .error-icon{fill:#552222;}#mermaid-svg-pamXGzapMzRYZthl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pamXGzapMzRYZthl .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-pamXGzapMzRYZthl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pamXGzapMzRYZthl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pamXGzapMzRYZthl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pamXGzapMzRYZthl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pamXGzapMzRYZthl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pamXGzapMzRYZthl .marker.cross{stroke:#333333;}#mermaid-svg-pamXGzapMzRYZthl svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pamXGzapMzRYZthl .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-pamXGzapMzRYZthl .cluster-label text{fill:#333;}#mermaid-svg-pamXGzapMzRYZthl .cluster-label span{color:#333;}#mermaid-svg-pamXGzapMzRYZthl .label text,#mermaid-svg-pamXGzapMzRYZthl span{fill:#333;color:#333;}#mermaid-svg-pamXGzapMzRYZthl .node rect,#mermaid-svg-pamXGzapMzRYZthl .node circle,#mermaid-svg-pamXGzapMzRYZthl .node ellipse,#mermaid-svg-pamXGzapMzRYZthl .node polygon,#mermaid-svg-pamXGzapMzRYZthl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-pamXGzapMzRYZthl .node .label{text-align:center;}#mermaid-svg-pamXGzapMzRYZthl .node.clickable{cursor:pointer;}#mermaid-svg-pamXGzapMzRYZthl .arrowheadPath{fill:#333333;}#mermaid-svg-pamXGzapMzRYZthl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-pamXGzapMzRYZthl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-pamXGzapMzRYZthl .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-pamXGzapMzRYZthl .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-pamXGzapMzRYZthl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-pamXGzapMzRYZthl .cluster text{fill:#333;}#mermaid-svg-pamXGzapMzRYZthl .cluster span{color:#333;}#mermaid-svg-pamXGzapMzRYZthl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-pamXGzapMzRYZthl :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u914d\u7f6elifespan\u53c2\u6570<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5b9a\u4e49\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5b9e\u73b0\u542f\u52a8\u65f6\u521d\u59cb\u5316\u903b\u8f91<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5b9e\u73b0\u5173\u95ed\u65f6\u6e05\u7406\u903b\u8f91<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u901a\u8fc7yield\u8fd4\u56de\u4e0a\u4e0b\u6587<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5c06lifespan\u4f20\u9012\u7ed9\u670d\u52a1\u5668<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u670d\u52a1\u5668\u521d\u59cb\u5316<\/span><\/p>\n<p>\u56fe\u6e90<\/p>\n<ul>\n<li>server.py<\/li>\n<li>server.py<\/li>\n<\/ul>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>server.py<\/li>\n<li>server.py<\/li>\n<\/ul>\n<h3>\u6570\u636e\u5e93\u8fde\u63a5\u793a\u4f8b\u5206\u6790<\/h3>\n<p>\u5728examples\/snippets\/servers\/lowlevel\/lifespan.py\u793a\u4f8b\u4e2d&#xff0c;\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u751f\u547d\u5468\u671f\u94a9\u5b50\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002\u793a\u4f8b\u5b9a\u4e49\u4e86\u4e00\u4e2aDatabase\u7c7b&#xff0c;\u5305\u542bconnect\u548cdisconnect\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u5efa\u7acb\u548c\u65ad\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u3002server_lifespan\u51fd\u6570\u4f5c\u4e3a\u751f\u547d\u5468\u671f\u7ba1\u7406\u5668&#xff0c;\u5728try\u5757\u4e2d\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5&#xff0c;\u5e76\u901a\u8fc7yield\u8bed\u53e5\u5c06\u5305\u542b\u6570\u636e\u5e93\u5b9e\u4f8b\u7684\u5b57\u5178\u4f20\u9012\u7ed9\u670d\u52a1\u5668\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>  #mermaid-svg-HlywSi6HaYDg8a85 {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-HlywSi6HaYDg8a85 .error-icon{fill:#552222;}#mermaid-svg-HlywSi6HaYDg8a85 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-HlywSi6HaYDg8a85 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-HlywSi6HaYDg8a85 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-HlywSi6HaYDg8a85 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-HlywSi6HaYDg8a85 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-HlywSi6HaYDg8a85 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-HlywSi6HaYDg8a85 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-HlywSi6HaYDg8a85 .marker.cross{stroke:#333333;}#mermaid-svg-HlywSi6HaYDg8a85 svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-HlywSi6HaYDg8a85 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-HlywSi6HaYDg8a85 text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-HlywSi6HaYDg8a85 .actor-line{stroke:grey;}#mermaid-svg-HlywSi6HaYDg8a85 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-HlywSi6HaYDg8a85 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-HlywSi6HaYDg8a85 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-HlywSi6HaYDg8a85 .sequenceNumber{fill:white;}#mermaid-svg-HlywSi6HaYDg8a85 #sequencenumber{fill:#333;}#mermaid-svg-HlywSi6HaYDg8a85 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-HlywSi6HaYDg8a85 .messageText{fill:#333;stroke:#333;}#mermaid-svg-HlywSi6HaYDg8a85 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-HlywSi6HaYDg8a85 .labelText,#mermaid-svg-HlywSi6HaYDg8a85 .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-HlywSi6HaYDg8a85 .loopText,#mermaid-svg-HlywSi6HaYDg8a85 .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-HlywSi6HaYDg8a85 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-HlywSi6HaYDg8a85 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-HlywSi6HaYDg8a85 .noteText,#mermaid-svg-HlywSi6HaYDg8a85 .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-HlywSi6HaYDg8a85 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-HlywSi6HaYDg8a85 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-HlywSi6HaYDg8a85 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-HlywSi6HaYDg8a85 .actorPopupMenu{position:absolute;}#mermaid-svg-HlywSi6HaYDg8a85 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-HlywSi6HaYDg8a85 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-HlywSi6HaYDg8a85 .actor-man circle,#mermaid-svg-HlywSi6HaYDg8a85 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-HlywSi6HaYDg8a85 :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>      FastMCP\u670d\u52a1\u5668<\/p>\n<p>      \u751f\u547d\u5468\u671f\u7ba1\u7406\u5668<\/p>\n<p>      \u6570\u636e\u5e93<\/p>\n<p>   \u542f\u52a8\u670d\u52a1\u5668<\/p>\n<p>   await Database.connect()<\/p>\n<p>   \u8fd4\u56de\u6570\u636e\u5e93\u5b9e\u4f8b<\/p>\n<p>   yield {&#034;db&#034; : db}<\/p>\n<p>   \u5904\u7406\u8bf7\u6c42<\/p>\n<p>   \u5173\u95ed\u670d\u52a1\u5668<\/p>\n<p>   await db.disconnect()<\/p>\n<p>   \u65ad\u5f00\u8fde\u63a5<\/p>\n<p>   \u6e05\u7406\u5b8c\u6210<\/p>\n<p>     FastMCP\u670d\u52a1\u5668<\/p>\n<p>     \u751f\u547d\u5468\u671f\u7ba1\u7406\u5668<\/p>\n<p>     \u6570\u636e\u5e93<\/p>\n<p>\u56fe\u6e90<\/p>\n<ul>\n<li>lifespan.py<\/li>\n<\/ul>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>lifespan.py<\/li>\n<\/ul>\n<h3>lifespan_wrapper\u673a\u5236\u89e3\u6790<\/h3>\n<p>lifespan_wrapper\u51fd\u6570\u8d1f\u8d23\u5c06FastMCP\u5b9e\u4f8b\u4f20\u9012\u7ed9\u751f\u547d\u5468\u671f\u7ba1\u7406\u5668\u3002\u8be5\u51fd\u6570\u63a5\u53d7FastMCP\u5b9e\u4f8b\u548c\u751f\u547d\u5468\u671f\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u9002\u914d\u5e95\u5c42MCPServer\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u5668\u3002\u5305\u88c5\u51fd\u6570\u5185\u90e8\u901a\u8fc7async with lifespan(app) as context\u8c03\u7528\u539f\u59cb\u7684\u751f\u547d\u5468\u671f\u51fd\u6570&#xff0c;\u5e76\u5c06FastMCP\u5b9e\u4f8b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012&#xff0c;\u5b9e\u73b0\u4e86\u6846\u67b6\u4e0e\u751f\u547d\u5468\u671f\u7ba1\u7406\u5668\u4e4b\u95f4\u7684\u96c6\u6210\u3002<\/p>\n<p>  #mermaid-svg-4RoDWfg0DQY6S41y {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-4RoDWfg0DQY6S41y .error-icon{fill:#552222;}#mermaid-svg-4RoDWfg0DQY6S41y .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4RoDWfg0DQY6S41y .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-4RoDWfg0DQY6S41y .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4RoDWfg0DQY6S41y .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4RoDWfg0DQY6S41y .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4RoDWfg0DQY6S41y .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4RoDWfg0DQY6S41y .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4RoDWfg0DQY6S41y .marker.cross{stroke:#333333;}#mermaid-svg-4RoDWfg0DQY6S41y svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4RoDWfg0DQY6S41y g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-4RoDWfg0DQY6S41y g.classGroup text .title{font-weight:bolder;}#mermaid-svg-4RoDWfg0DQY6S41y .nodeLabel,#mermaid-svg-4RoDWfg0DQY6S41y .edgeLabel{color:#131300;}#mermaid-svg-4RoDWfg0DQY6S41y .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-4RoDWfg0DQY6S41y .label text{fill:#131300;}#mermaid-svg-4RoDWfg0DQY6S41y .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-4RoDWfg0DQY6S41y .classTitle{font-weight:bolder;}#mermaid-svg-4RoDWfg0DQY6S41y .node rect,#mermaid-svg-4RoDWfg0DQY6S41y .node circle,#mermaid-svg-4RoDWfg0DQY6S41y .node ellipse,#mermaid-svg-4RoDWfg0DQY6S41y .node polygon,#mermaid-svg-4RoDWfg0DQY6S41y .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4RoDWfg0DQY6S41y .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-4RoDWfg0DQY6S41y g.clickable{cursor:pointer;}#mermaid-svg-4RoDWfg0DQY6S41y g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-4RoDWfg0DQY6S41y g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-4RoDWfg0DQY6S41y .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-4RoDWfg0DQY6S41y .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-4RoDWfg0DQY6S41y .dashed-line{stroke-dasharray:3;}#mermaid-svg-4RoDWfg0DQY6S41y #compositionStart,#mermaid-svg-4RoDWfg0DQY6S41y .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #compositionEnd,#mermaid-svg-4RoDWfg0DQY6S41y .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #dependencyStart,#mermaid-svg-4RoDWfg0DQY6S41y .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #dependencyStart,#mermaid-svg-4RoDWfg0DQY6S41y .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #extensionStart,#mermaid-svg-4RoDWfg0DQY6S41y .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #extensionEnd,#mermaid-svg-4RoDWfg0DQY6S41y .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #aggregationStart,#mermaid-svg-4RoDWfg0DQY6S41y .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y #aggregationEnd,#mermaid-svg-4RoDWfg0DQY6S41y .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4RoDWfg0DQY6S41y .edgeTerminals{font-size:11px;}#mermaid-svg-4RoDWfg0DQY6S41y :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><span class=\"edgeLabel\">\u4f7f\u7528<\/span><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><span class=\"edgeLabel\">\u9002\u914d<\/span><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><span class=\"edgeLabel\">\u5305\u542b<\/span><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">FastMCP<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;name : str<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;instructions : str<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;lifespan : Callable<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;_mcp_server : MCPServer<\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">lifespan_wrapper<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;app : FastMCP<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;lifespan : Callable<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;wrap(_ : MCPServer)<\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">MCPServer<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;lifespan : Callable<\/span><\/p>\n<p>         <span class=\"nodeLabel\">&#043;request_context : RequestContext<\/span><\/p>\n<p>\u56fe\u6e90<\/p>\n<ul>\n<li>server.py<\/li>\n<\/ul>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>server.py<\/li>\n<\/ul>\n<h3>lifespan_context\u8bbf\u95ee\u65b9\u5f0f<\/h3>\n<p>\u5728\u8bf7\u6c42\u5904\u7406\u8fc7\u7a0b\u4e2d&#xff0c;\u53ef\u4ee5\u901a\u8fc7server.request_context\u8bbf\u95ee\u751f\u547d\u5468\u671f\u4e0a\u4e0b\u6587\u3002RequestContext\u7c7b\u5305\u542b\u4e00\u4e2a\u6cdb\u578b\u5b57\u6bb5lifespan_context&#xff0c;\u7528\u4e8e\u5b58\u50a8\u751f\u547d\u5468\u671f\u7ba1\u7406\u5668\u901a\u8fc7yield\u8bed\u53e5\u8fd4\u56de\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u5728\u5de5\u5177\u51fd\u6570\u4e2d&#xff0c;\u53ef\u4ee5\u4f7f\u7528ctx &#061; server.request_context\u83b7\u53d6\u8bf7\u6c42\u4e0a\u4e0b\u6587&#xff0c;\u7136\u540e\u901a\u8fc7ctx.lifespan_context[&#034;db&#034;]\u8bbf\u95ee\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u8d44\u6e90\u3002<\/p>\n<p>  #mermaid-svg-zyxWvyo2vWMUgEul {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .error-icon{fill:#552222;}#mermaid-svg-zyxWvyo2vWMUgEul .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zyxWvyo2vWMUgEul .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-zyxWvyo2vWMUgEul .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zyxWvyo2vWMUgEul .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zyxWvyo2vWMUgEul .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zyxWvyo2vWMUgEul .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zyxWvyo2vWMUgEul .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zyxWvyo2vWMUgEul .marker.cross{stroke:#333333;}#mermaid-svg-zyxWvyo2vWMUgEul svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zyxWvyo2vWMUgEul .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .cluster-label text{fill:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .cluster-label span{color:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .label text,#mermaid-svg-zyxWvyo2vWMUgEul span{fill:#333;color:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .node rect,#mermaid-svg-zyxWvyo2vWMUgEul .node circle,#mermaid-svg-zyxWvyo2vWMUgEul .node ellipse,#mermaid-svg-zyxWvyo2vWMUgEul .node polygon,#mermaid-svg-zyxWvyo2vWMUgEul .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zyxWvyo2vWMUgEul .node .label{text-align:center;}#mermaid-svg-zyxWvyo2vWMUgEul .node.clickable{cursor:pointer;}#mermaid-svg-zyxWvyo2vWMUgEul .arrowheadPath{fill:#333333;}#mermaid-svg-zyxWvyo2vWMUgEul .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zyxWvyo2vWMUgEul .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zyxWvyo2vWMUgEul .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-zyxWvyo2vWMUgEul .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-zyxWvyo2vWMUgEul .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zyxWvyo2vWMUgEul .cluster text{fill:#333;}#mermaid-svg-zyxWvyo2vWMUgEul .cluster span{color:#333;}#mermaid-svg-zyxWvyo2vWMUgEul div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-zyxWvyo2vWMUgEul :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u6536\u5230\u8bf7\u6c42<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u83b7\u53d6\u8bf7\u6c42\u4e0a\u4e0b\u6587ctx &#061; server.request_context<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u8bbf\u95ee\u751f\u547d\u5468\u671f\u4e0a\u4e0b\u6587db &#061; ctx.lifespan_context[&#039;db&#039;]<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u4f7f\u7528\u8d44\u6e90\u6267\u884c\u64cd\u4f5c<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u8fd4\u56de\u7ed3\u679c<\/span><\/p>\n<p>\u56fe\u6e90<\/p>\n<ul>\n<li>context.py<\/li>\n<li>lifespan.py<\/li>\n<\/ul>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>context.py<\/li>\n<li>lifespan.py<\/li>\n<\/ul>\n<h3>\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u6a21\u677f<\/h3>\n<p>\u4ee5\u4e0b\u4e3a\u914d\u7f6e\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u7684\u4ee3\u7801\u6a21\u677f&#xff1a;<\/p>\n<p>  #mermaid-svg-LpSY1mJgMYGi2ZjT {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .error-icon{fill:#552222;}#mermaid-svg-LpSY1mJgMYGi2ZjT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-LpSY1mJgMYGi2ZjT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .marker.cross{stroke:#333333;}#mermaid-svg-LpSY1mJgMYGi2ZjT svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .cluster-label text{fill:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .cluster-label span{color:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .label text,#mermaid-svg-LpSY1mJgMYGi2ZjT span{fill:#333;color:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .node rect,#mermaid-svg-LpSY1mJgMYGi2ZjT .node circle,#mermaid-svg-LpSY1mJgMYGi2ZjT .node ellipse,#mermaid-svg-LpSY1mJgMYGi2ZjT .node polygon,#mermaid-svg-LpSY1mJgMYGi2ZjT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .node .label{text-align:center;}#mermaid-svg-LpSY1mJgMYGi2ZjT .node.clickable{cursor:pointer;}#mermaid-svg-LpSY1mJgMYGi2ZjT .arrowheadPath{fill:#333333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-LpSY1mJgMYGi2ZjT .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-LpSY1mJgMYGi2ZjT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-LpSY1mJgMYGi2ZjT .cluster text{fill:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT .cluster span{color:#333;}#mermaid-svg-LpSY1mJgMYGi2ZjT div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-LpSY1mJgMYGi2ZjT :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5b9a\u4e49\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#064;asynccontextmanagerasync def custom_lifespan(&#8230;)<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5728try\u5757\u4e2d\u6267\u884c\u542f\u52a8\u903b\u8f91<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u521d\u59cb\u5316\u7f13\u5b58\u5ba2\u6237\u7aef<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u521d\u59cb\u5316\u5916\u90e8\u670d\u52a1\u8fde\u63a5<\/span><\/p>\n<p>         <span class=\"nodeLabel\">yield \u8fd4\u56de\u4e0a\u4e0b\u6587\u5b57\u5178<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5728finally\u5757\u4e2d\u6267\u884c\u6e05\u7406\u903b\u8f91<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5173\u95ed\u7f13\u5b58\u8fde\u63a5<\/span><\/p>\n<p>         <span class=\"nodeLabel\">\u5173\u95ed\u5916\u90e8\u670d\u52a1\u8fde\u63a5<\/span><\/p>\n<p>         <span class=\"nodeLabel\">End<\/span><\/p>\n<p>\u56fe\u6e90<\/p>\n<ul>\n<li>lifespan_example.py<\/li>\n<\/ul>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>lifespan_example.py<\/li>\n<\/ul>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p>\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u9002\u7528\u4e8e\u591a\u79cd\u8d44\u6e90\u7ba1\u7406\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7ba1\u7406&#xff1a;\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u521b\u5efa\u8fde\u63a5\u6c60&#xff0c;\u5173\u95ed\u65f6\u4f18\u96c5\u5730\u91ca\u653e\u6240\u6709\u8fde\u63a5<\/li>\n<li>\u7f13\u5b58\u5ba2\u6237\u7aef\u7ba1\u7406&#xff1a;\u521d\u59cb\u5316Redis\u6216Memcached\u5ba2\u6237\u7aef\u8fde\u63a5<\/li>\n<li>\u5916\u90e8\u670d\u52a1\u8fde\u63a5&#xff1a;\u5efa\u7acb\u4e0e\u7b2c\u4e09\u65b9API\u3001\u6d88\u606f\u961f\u5217\u7b49\u5916\u90e8\u670d\u52a1\u7684\u6301\u4e45\u8fde\u63a5<\/li>\n<li>\u6587\u4ef6\u53e5\u67c4\u7ba1\u7406&#xff1a;\u6253\u5f00\u65e5\u5fd7\u6587\u4ef6\u6216\u5176\u4ed6\u9700\u8981\u957f\u671f\u6301\u6709\u7684\u6587\u4ef6\u53e5\u67c4<\/li>\n<li>\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406&#xff1a;\u542f\u52a8\u540e\u53f0\u5b9a\u65f6\u4efb\u52a1\u6216\u5de5\u4f5c\u7ebf\u7a0b<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u573a\u666f\u90fd\u9700\u8981\u786e\u4fdd\u8d44\u6e90\u5728\u670d\u52a1\u5668\u751f\u547d\u5468\u671f\u5185\u6b63\u786e\u521d\u59cb\u5316\u548c\u6e05\u7406&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u548c\u8fde\u63a5\u4e2d\u65ad\u95ee\u9898\u3002<\/p>\n<p>\u672c\u8282\u6765\u6e90<\/p>\n<ul>\n<li>lifespan.py<\/li>\n<li>lifespan_example.py<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0lifespan\u53c2\u6570\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u793a\u4f8b\u5206\u6790lifespan_wrapper\u673a\u5236\u89e3\u6790lifespan_context\u8bbf\u95ee\u65b9\u5f0f\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u6a21\u677f\u5e94\u7528\u573a\u666f<br \/>\n\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0<br \/>\nFastMCP\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u670d\u52a1\u5668\u542f\u52a8\u548c\u5173\u95ed\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u8d44\u6e90\u7ba1\u7406\u64cd\u4f5c\u3002\u8be5\u673a\u5236\u901a\u8fc7lifespan\u53c2\u6570\u914d\u7f6e\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u5b9e\u73b0\u670d\u52a1\u5668\u751f\u547d\u5468\u671f\u7684\u7cbe\u7ec6\u5316\u63a7\u5236\u3002\u751f\u547d\u5468\u671f\u94a9\u5b50\u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u9700\u8981\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u521d\u59cb\u5316\u3001\u5728\u5173\u95ed\u65f6\u6e05\u7406\u7684\u8d44\u6e90&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[72,73,43],"topic":[],"class_list":["post-96779","post","type-post","status-publish","format-standard","hentry","category-server","tag-fastapi","tag-mcp","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/96779.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0lifespan\u53c2\u6570\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u793a\u4f8b\u5206\u6790lifespan_wrapper\u673a\u5236\u89e3\u6790lifespan_context\u8bbf\u95ee\u65b9\u5f0f\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u6a21\u677f\u5e94\u7528\u573a\u666f \u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0 FastMCP\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u670d\u52a1\u5668\u542f\u52a8\u548c\u5173\u95ed\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u8d44\u6e90\u7ba1\u7406\u64cd\u4f5c\u3002\u8be5\u673a\u5236\u901a\u8fc7lifespan\u53c2\u6570\u914d\u7f6e\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u5b9e\u73b0\u670d\u52a1\u5668\u751f\u547d\u5468\u671f\u7684\u7cbe\u7ec6\u5316\u63a7\u5236\u3002\u751f\u547d\u5468\u671f\u94a9\u5b50\u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u9700\u8981\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u521d\u59cb\u5316\u3001\u5728\u5173\u95ed\u65f6\u6e05\u7406\u7684\u8d44\u6e90&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/96779.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-28T03:16:48+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/96779.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/96779.html\",\"name\":\"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-28T03:16:48+00:00\",\"dateModified\":\"2026-08-28T03:16:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/96779.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/96779.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/96779.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/96779.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0lifespan\u53c2\u6570\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u793a\u4f8b\u5206\u6790lifespan_wrapper\u673a\u5236\u89e3\u6790lifespan_context\u8bbf\u95ee\u65b9\u5f0f\u81ea\u5b9a\u4e49\u751f\u547d\u5468\u671f\u94a9\u5b50\u6a21\u677f\u5e94\u7528\u573a\u666f \u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u6982\u8ff0 FastMCP\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u670d\u52a1\u5668\u542f\u52a8\u548c\u5173\u95ed\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u8d44\u6e90\u7ba1\u7406\u64cd\u4f5c\u3002\u8be5\u673a\u5236\u901a\u8fc7lifespan\u53c2\u6570\u914d\u7f6e\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u5b9e\u73b0\u670d\u52a1\u5668\u751f\u547d\u5468\u671f\u7684\u7cbe\u7ec6\u5316\u63a7\u5236\u3002\u751f\u547d\u5468\u671f\u94a9\u5b50\u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u9700\u8981\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u521d\u59cb\u5316\u3001\u5728\u5173\u95ed\u65f6\u6e05\u7406\u7684\u8d44\u6e90&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/96779.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-28T03:16:48+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/96779.html","url":"https:\/\/www.wsisp.com\/helps\/96779.html","name":"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-28T03:16:48+00:00","dateModified":"2026-08-28T03:16:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/96779.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/96779.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/96779.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7b2c\u56db\u7ae0 FastMCP \u670d\u52a1\u5668\u6846\u67b6\u4e4b\u751f\u547d\u5468\u671f\u94a9\u5b50"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/96779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=96779"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/96779\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=96779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=96779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=96779"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=96779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}